【Python】练习---循环语句

tech2024-04-02  67

""" Demo 37 数据:电脑产生随机数字 com,用户输入数字user 步骤: 1、电脑随机生成一个数字(0-100) 2、提示用户输入一个数字 3、对数字进行比较 当com>user,提示数字太高 当com<user,提示数字太低 当com=user,提示猜对了输出数字,程序结束 """ import random com = random.randint(0,100) while True : user = int(input("Enter your guess:")) if com < user : print("Your guess is too high!") elif com == user : print("Yes,the number is ",com) break elif com > user: print("Your guess is too low!")

测试结果:

 

""" Demo 38 数据:两个整数num1和num2,最大公约数gcd,数字k(2-min(num1,num2) 步骤: 1、提示用户输入两个整数 2、从数字2开始到min(num1,num2)找两数的公约数 若num1 % k==0 and num2 % k ==0 则gcd=k 当找完所有可能公约数后,最后的gcd为两者的最大公约数 """ num1,num2 = eval(input("请输入两个整数:")) gcd = 1 for k in range(2,min(num1, num2) +1 ): if num1 % k == 0 and num2 % k == 0 : gcd = k print("%d和%d的最大公约数是:%d" %(num1,num2,gcd))

测试结果:

 

""" Demo 39 数据:1000000个点total,落入圆内点数sum 步骤: 1、每次循环先随机生成一个坐标x∈[-1,1],y∈[-1,1] 2、点落入圆内sum+1 3、循环结束后,计算sum/total*4 """ import random sum = 0 total = 1000000 for i in range(0,total): x = random.random() * 2 - 1 y = random.random() * 2 - 1 distance = (x**2 + y**2) ** 0.5 if distance <= 1: sum += 1 pi = sum / total * 4 print(pi)

测试结果:

 

""" Demo 40 数据:正数positives,负数negatives,总和sum,平均值average,正数个数total 步骤: 1、提示用户输入一个数字 2、对数字进行判断 如果该数字为0 则循环结束 如果该数字 > 0 positives += 1 sum += 数字 继续1 如果该数字 < 0 negatives += 1 sum += 数字 继续1 3.计算平均值 = 总和 / (正 + 负) 如果总个数为0则输出没有任何数字输入 """ positives = 0 negatives = 0 sum = 0 average = 0 while True: num = int(input("Enter an integer,the input ends if it is 0:")) if num == 0 : break elif num > 0 : positives += 1 elif num < 0 : negatives += 1 sum += num total = positives + negatives if total == 0 : print("Your didn't enter any number") print("The number of positives is ",positives) print("The number of negatives is ",negatives) print("The sum is ",sum) print("The average is ",sum / total)

测试结果:

 

""" Demo 41 数据:一个整数num 思路: flag若为false,则表示循环被break,说明找到该数字的因子 flag若最后为True,则表示循环正常结束,没有找到数字的因子,则直接输出该数字 """ num = int(input("请输入一个整数:")) while num != 1 : flag = True for i in range(2,num // 2 + 1): if num % i == 0: print(i,end=' ') num = num // i flag = False break if flag : print(num) break

测试结果:

 

""" Demo 42 """ num = int(input("Enter the number of lines:")) for i in range(0,num): for k in range(num-i,0,-1): print(" ",end= " ") for j in range(-i,i+1): print(abs(j)+1,end=" ") print()

测试结果:

 

""" Demo 43 """ print("模式A") for i in range(1,7): for j in range(1,i+1): print(j,end=' ') print() print("模式B") for i in range(7,1,-1): for j in range(1,i): print(j,end=' ') print() print("模式C") for i in range(0,6): for k in range(6-i,0,-1): print(" ",end = " ") for j in range(i+1,0,-1): print(j,end = " ") print() print("模式D") for i in range(7,0,-1): for k in range(0,7-i): print(' ',end=' ') for j in range(1,i): print(j,end=' ') print()

测试结果:

 

""" Demo 44 """ for i in range(0,8): for k in range(8-i,0,-1): print(" ",end="") for j in range(-i,i+1): print(2 **(i - abs(j)),end=" ") print()

测试结果:

 

""" Demo 45 """ count = 0 for i in range(2,1001): for j in range(2,i): if i % j == 0 : break else: print(i,end='\t') count += 1 if count % 8 == 0: print()

测试结果:

 

""" Demo 46 """ for j in range(10000,100001,10000): sum = 0 for i in range(1,j+1): total = (-1) ** (i + 1) / (2 * i - 1) sum += total pi = sum * 4 print(pi)

测试结果:

 

""" Demo 47 """ e = 1 item = 1 for n in range(10000, 100001, 10000): for i in range(1, n + 1): item *= i e += 1 / item print("当i=%d时,e=%f" % (n, e))

测试结果:

 

""" Demo 48 """ for i in range(2,10001): sum = 1 for j in range(2,i // 2 + 1): if i % j == 0 : sum += j if sum == i : print(sum)

测试结果:

 

""" Demo 49 com 电脑,user 用户 剪刀 0 石头 1 布 2 若赢:0-2=-2 or 1 (2-1,0-1) """ import random flag1 = 0 flag2 = 0 while True: user = int(input("请输入一个数字(0:剪刀;1:石头;2:布)")) com = random.randint(0,2) if user - com == 1 or user - com == -2 : print("此局用户赢!") flag1 += 1 elif com == user : print("此局平局!") else: print("此局电脑赢!") flag2 += 1 if flag1 > 2 or flag2 > 2 : if flag1 > flag2: print("恭喜电脑获得最终胜利!") break if flag1 < flag2: print("恭喜电脑获得最终胜利!") break

测试结果:

 

""" Demo 50 """ count = 0 max = 0 while True: num = int(input("Enter a number(0:for end of input):")) if num == 0 : break if num > max : max = num count = 1 elif num == max : count += 1 print("The largest number is ",max) print("The occurrence count of the largest number is ",count)

测试结果:

 

""" Demo 51 s ---商 y ---余数 """ y = 0 b = '' num = int(input("请输入一个十进制整数:")) if num == 0: b = '0' while num : s = num // 2 y = num % 2 num = s b = str(y) + b print("二进制数为0b",b)

测试结果:

 

""" Demo 52 思路: 1、提示用户输入一个十进制整数 2、将十进制转为十六进制数 若输入的十进制数是0,则输出0 若不为0则循环: num // 16 若等于0,退出循环 num // 16 不等于0,求出num % 16余数,拼接余数 若余数为10---A,11---B,12---C,13---D,14---E,15---F 3、输出结果 """ number = int(input("Enter a number:")) hexStr = "" while number != 0: a = number % 16 if a < 10: hexStr = str(a) + hexStr else: hexStr = chr(55 + a) + hexStr number //= 16 print(hexStr)

测试结果:

 

""" Demo 53 数据:有1000000个点 步骤: 1、随机设定坐标在x∈[-1,1] and y∈[-1,1] 2、当 x∈[-1,0] 并且 y∈[-1,1] 和 x∈[0,1]并且y∈[0,1]并且x+y<=1时,落入奇数区域 3、求概率 """ import random num = 0 for i in range(0,1000001): x = random.random() * 2 - 1 y = random.random() * 2 - 1 if -1 <= x <= 0 and -1 <= y <= 1: num += 1 elif 0 <= x <= 1 and 0 <= y <= 1 and x + y <= 1: num += 1 p = num / 1000000 print("这个飞镖落在奇数区域里的概率是:%.5f"%p)

测试结果:

 

""" Demo 54 """ count = 0 for year in range(2001,2101): if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: print(year,end = " ") count += 1 if count % 10 == 0 : print()

测试结果:

 

 

最新回复(0)