循环语句
第三十七题
import random
com = random.randint(0, 100)
while True:
num = int(input("Enter a number:"))
if num == com:
print("Yes ,the number is", com)
break
elif num > com:
print("Your guess is too high")
else:
print("Your guess is too low")
第三十八题
while True:
num1, num2 = eval(input("Enter two numbers:"))
k = min(num1, num2)
gcd = 1
for i in range(2, k + 1):
if num1 % i == 0 and num2 % i == 0:
gcd = i
print(gcd)
第三十九题
import random
num = 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:
num += 1
pi = num / total * 4
print(pi)
第四十题
positives = 0
negatives = 0
sum = 0
while True:
num = int(input("Enter a number:"))
if num == 0:
break;
elif num > 0:
positives += 1
else :
negatives += 1
sum += num
total = positives + negatives
if total == 0:
print("You didn't enter any number")
else :
print("The number of positives",positives)
print("The number of negatives",negatives)
print("The sum is",sum)
print("The average is",sum / total)
第四十一题
number = int(input("Enter a number:"))
while number != 1:
flag = True
for i in range(2, number // 2 + 1):
if number % i == 0:
print(i, end=", ")
number = number // i
flag = False
break
if flag :
print(number,end=", ")
break
第四十二题
while True :
number = int(input("Enter a number:"))
for i in range(0, number):
for k in range(number - i, 0, -1):
print(" ",end=" ")
for j in range(-i, i + 1):
print(abs(j) + 1, end=" ")
print()
第四十三题
print("模式A")
while True:
for i in range (1,7):
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
print("模式B")
while True:
for i in range (6,0,-1):
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
print("模式C")
while True:
for i in range (1,7):
for j in range(6, 0,-1):
if j <= i:
print(j,end="\t")
else :
print("",end= "\t")
print()
break
print()
print("模式D")
while True:
for i in range (6,0,-1):
a = 6 - i
print("\t" * a,end ="")
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
第四十四题
while True :
number = int(input("Enter a number:"))
for i in range(0, number):
for k in range(number - i, 0, -1):
print("",end="\t")
for j in range(1,i + 1):
print(2 ** (j-1), end="\t")
for k in range(i + 1,0,-1):
print(2 ** (k-1),end="\t")
print()
第四十五题
count = 0
for num in range(2,1001):
for j in range(2, num):
if num % j == 0:
break;
else:
print(num,end = "\t")
count += 1
if count % 8 == 0:
print()
第四十六题
for j in range(10000,100001,10000):
sum = 0
for i in range(1,j + 1):
sum += ((-1) ** (i + 1)) / (2 * i -1)
PI = 4 * sum
print("i = %d 时, PI = %.9f"%(j,PI))
第四十七题
for j in range(10000,100001,10000):
e = 1
item = 1
for i in range(1,j + 1):
item *= i
e += 1 / item
print("i = %d 时, e = %.19f"%(j,e))
第四十八题
for i in range(1,10001):
sum = 0
for j in range (1,i):
if i % j == 0:
sum +=j
if i == sum:
print(i)
第四十九题
import random
user_sum = 0
com_sum = 0
while True :
com = random.randint(0,2)
user = int(input("请输入石头(0),剪刀(1),布(2)所对应的数字:"))
if user - com == -1 or user - com == 2:
user_sum += 1
if user_sum == 2:
print("玩家赢了")
break
elif user - com == -2 or user - com == 1:
com_sum += 1
if com_sum == 2:
print("电脑赢了")
break
第五十题
max = 0
count = 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)
第五十一题
number = int(input("Enter a number:"))
binStr = ""
while number != 0:
a = number % 2
binStr = str(a) + binStr
number //= 2
print(binStr)
第五十二题
number = int(input("Enter a number:"))
hexStr = ""
while number != 0:
a = number % 16
if a < 10:
hexStr = str(a) + hexStr
else:
hexStr = chr(87 + a) + hexStr
number //= 16
print(hexStr)
第五十三题
import random
sum = 0
for i in range (0, 1000001):
x = random.random() * random.choice([-1,1])
y = random.random() * random.choice([-1,1])
if x<= 0 or ( x >= 0 and y >= 0 and (y / (1 - x) >= 1)):
sum += 1
a = sum / 1000000
print("1000000个随机点落在奇数区域的概率是%.12f"%a)
第五十四题
count = 0
for y in range(2001, 2100 + 1):
if y % 400 == 0 or y % 4 == 0 and y % 100 != 0:
print(y, end=" ")
count += 1
if count % 10 == 0:
print()
|