Python simple programming exercises

    Python already learned the most basic syntax: variable types, operators, expressions, branched structure, cyclic structure. You may have to write some simple programming through these topics, but also the time to consolidate their own knowledge of it!

    The following programming problem is found on GitHub a very useful learning resources , recommended the entry of new partners focus on small Luo Hao great God, because he's tutorials super nice! We can help you take a lot less detours!

python operator input functions and their use

  1. Fahrenheit is converted to degrees Celsius (Tip: Fahrenheit to Celsius conversion equation is: C = (F - 32) /1.8)

#F_C.py

'''
    练习1:华氏温度转换为摄氏温度
    提示:华氏温度到摄氏温度的转换公式为:$C=(F - 32) \div 1.8$。
'''

#提示用户输入华氏温度,并转化为对应的数值
f=float(input("请输入华氏温度:"))
#print(f)

#对温度进行转化
c=(f-32)/1.8

'''
    数据的输出
    错误输出:print("当前华氏温度:%f,对应的摄氏温度为:%f",f,c)
    Python中数据的输入与输出
    输入:input函数
    输出:print函数,使用占位符语法:百分号代替逗号,输出的变量要用括号
'''
print("当前华氏温度:%.2f,对应的摄氏温度为:%f" % (f,c))
  1. Enter a radius circle perimeter and the area calculation.

#CirculOperate.py

'''
    编程练习:输入圆的半径计算计算周长和面积
    时间:2020年1月13日11:05:13
    作者:Dragon
'''

r=float(input("请输入圆的半径:"))
pi=3.14;
s=2*pi*r;
area=pi*r*r;
print("半径为",r,'的周长为:',s,' 面积为:',area)
  1. Enter the year is not a leap year judgment.

#IsRunYear.py

'''
    编程练习:输入年份判断是不是闰年,如果是闰年输出True 否则输出False
    时间:2020年1月13日11:10:46
    作者:Dragon
    提示:能被4且不能被100或者400整除的数
'''

year=int(input("please input your year:"))
if (year % 4 == 0 and year % 100 != 0) or \
           year % 400 == 0:
    print(year,"True")
else:
    print(year,"False")

Using a branched structure if-else statements, elif and else

  1. User Authentication

#UserValid.py

'''
    编程练习 if-else语句
    描述:用户身份验证
          正确的账号 admin 和密码 0000
    验证成功输出:welcome admin!
    验证失败输出:
        a.user name is fail
        b.password is fail
    加强版:循环验证,直到验证正确
    作者:Dragon
'''

username=input("please input your username:")
if (username!="admin"):
    print("user name is fail")
else:
    password=input("please input your password:")
    if password!="0000":
        print("password is fail")
    else:
        print("welcome admin!")
  1. Piecewise function evaluation
       
                            3x - 5 (x> 1)
    piecewise function F (X) = X 2 + (-1 <= X <=. 1)
                            5X +. 3 (X <-1)

#PiecewiseFunction.py
'''
    编程实战:分段函数求值
	               3x - 5  (x > 1)
    分段函数f(x) =  x + 2   (-1 <= x <= 1)
               	   5x + 3  (x < -1)
    时间:2020年1月13日11:34:11
    作者:Dragon
'''

x=float(input("请输入你的数值:"))
if x<-1:
    f=5*x+3;
elif x<=1:
    f=x+2;
else:
    f=3*x-5;
print("对应的结果为:",f)

 

  1. Percentile score converted into hierarchical Results:
        If the entered score 90 points or more (including 90 minutes) the output A;
        80 -90 minutes (90 minutes excluding) the output B;
        70 min -80 minutes (excluding 80) output C;
        60 minutes -70 minutes (70 minutes excluding) the output D;
        60 minutes following output E.

#ScoreToLevel.py

'''
编程练习:百分制成绩转换为等级制成绩:
	     如果输入的成绩在90分以上(含90分)输出A;
	     80分-90分(不含90分)输出B;
	     70分-80分(不含80分)输出C;
	     60分-70分(不含70分)输出D;
	     60分以下输出E。
时间:2020年1月13日11:40:06
作者:Dragon
'''

score=float(input("请输入您的成绩:"))
if score<60:
    print("成绩为E,您要再多多努力!")
elif score<70:
    print("成绩为D,您要再多多努力!")
elif score<80:
    print("成绩为c,加油!")
elif score<90:
    print("成绩为B,不错哦~")
else:
    print("成绩为A,学霸啊!")

 

Loop structure: `for-in` loop, one is` while` cycle, using three common mode range function

  1. 1 with the for-loop 100 sums

#AddByFor.py

'''
    用for循环实现1~100求和
    时间:2020年1月14日10:54:22
    作者:Dragon
    注意for-in的语法格式
    range的三种使用方法:
        range(101)可以产生一个0到100的整数序列。
        range(1, 100)可以产生一个1到99的整数序列。
        range(1, 100, 2)可以产生一个1到99的奇数序列,其中2是步长,即数值序列的增量。
'''

sum=0;
for x in range(0,101):
    sum+=x
print(sum)
  1. Guessing game
          computer a random number between 1 and 100 by a person to guess
          the computer prompt digital guessed are larger / smaller / guessed
          attention introduced import random, random.randint () function is used

#GuessNumber.py
import random   #导入random

'''
    描述:计算机出一个1~100之间的随机数由人来猜
          计算机根据人猜的数字分别给出提示大一点/小一点/猜对了
    时间:2020年1月14日11:01:36
    作者:Dragon
    注意:生成随机数需要导入import random,random.randint()函数的使用
'''

#系统生成1-100随机数字
randomNumber=random.randint(1,100)
#print("我是作弊器:随机数是:",randomNumber)

#提示用户输入数字
while True:
    userNumber=int(input("请输入您的数字:"))
    if(userNumber<randomNumber):
        print("再大一点!")
    elif userNumber>randomNumber:
        print("再小一点!")
    else:
        print("猜对了")
        break
  1. The output of the multiplication tables (multiplication table)

#PrintMulTable.py

'''
    描述:输出乘法口诀表(九九表)
    时间:2020年1月14日11:11:19
    作者:Dragon
    思路:先思考一下乘法表长什么样,该怎么输出!
          一行一行输出,列成递增
          行:1-9
          列:1-9
          输出格式:a*b=c,不换行输出且制表
          考虑完毕,动手!
'''

for row in range(1,10):
    for cow in range(1,10):
        if cow <= row:
            print("%d * %d = %d \t" %(cow,row,row*cow),end="")
            #print("%d * %d = %d " %(cow,row,row*cow),end="\t"),两种方式皆可
        else:
            break
    print()     #换行
  1. Enter a positive integer is not a prime number is determined

#IsPrime.py

'''
    描述:
    时间:2020年1月14日11:28:48
    作者:Dragon
    思路:素数指的是只能被1和自身整除的大于1的整数
          通过循环看其有没有其他因子
'''

number=int(input("请输入要判断的数字:"))
flag=0;
for x in range(2,number):
    #print("进入循环,当前x为:",x)
    #print("number%x==0   为:",number/x==0)
    if(number%x==0):
        print("不是素数!")
        flag=1
        break

if flag==0:
    print("是素数!")

 

  1. Print triangular pattern as shown below.

    ```
    *
    **
    ***
    ****
    *****
    ```

    ```
        *
       **
      ***
     ****
    *****
    ```

    ```
        *
       ***
      *****
     *******
    *********
    ```
     

    
    '''
        描述:打印三角形图案。
        时间:2020年1月14日12:17:02
        作者:Dragon
    '''
    
    '''
    打印第一种图案
    *
    **
    ***
    ****
    *****
    '''
    for r1 in range(1,6):
        for c1 in range(1,6):
            if c1<=r1:
                print("*",end="")
        print()
    
    '''
    打印第二种图案
        *
       **
      ***
     ****
    *****
    '''
    for r2 in range(1,6):
        for c2 in range(1,6):
            if (6-c2) > r2:
                print(" ",end="")
            else:
                print("*", end="")
        print()
    
    '''
    打印第三种图案:1,3,5,7,9
        *    2i-1个,n行,【2n-(2i-1)】/2
       ***
      *****
     *******
    *********
    '''
    for i in range(5):
        for _ in range(5 - i - 1):
            print(' ', end='')
        for _ in range(2 * i + 1):
            print('*', end='')
        print()

     

 

Published 115 original articles · won praise 52 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_36631076/article/details/103921141