import random
com = random.randint(0,100)# [0,100] range(0,100) # [0,100)whileTrue:
num =int(input("Enter a number:"))if num == com:print("Yes ,the number is", com)breakelif num > com:print("Your guess is too high")else:print("Your guess is too low")
第三十八题
whileTrue:
num1, num2 =eval(input("Enter two numbers:"))
k =min(num1, num2)
gcd =1for i inrange(2, k +1):if num1 % i ==0and num2 % i ==0:
gcd = i
print(gcd)
第三十九题
import random
num =0
total =1000000for i inrange(0, total):# [0,1) * 2 => [0,2) - 1 =>[-1,1)
x = random.random()*2-1
y = random.random()*2-1
distance =(x **2+ y **2)**0.5if distance <=1:
num +=1
pi = num / total *4print(pi)
第四十题
positives =0
negatives =0sum=0whileTrue:
num =int(input("Enter a number:"))if num ==0:break;elif num >0:
positives +=1else:
negatives +=1sum+= 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