name = input('What is your name?')
print('Welcome to my quiz',name)
guess = 0
tries = 0
answer = 5
score = 0
while guess != answer and tries < 2:
guess = int(input("10/2 is..."))
if guess == answer:
print ("Correct")
score = score + 1
else:
print ("Incorrect")
score = score + 0
tries = tries + 1
guess = 0
tries = 0
answer = 25
while guess != answer and tries <2:
guess = int(input("5*5 is..."))
if guess == answer:
print("Correct")
score = score + 1
else:
print("Incorrect")
score = score + 0
tries = tries + 1
print("Thank you for playing",name,". You scored",score,"points")
我试图用随机数循环问题,但我不知道该怎么做。如何进行测验,使用随机数询问用户乘法,加法,减法和除法问题并记录他们的分数。
答案 0 :(得分:2)
>>> import random
>>> random.randint(0,10)
3
>>> random.randint(0,10)
8
>>> random.randint(0,10)
10
您可以使用python random库为您的问题生成随机数。
>>> help(random.randint)
Help on method randint in module random:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
因此,您可以为 random.randint 方法提供范围,并且每次调用它时都会为您生成唯一值。
答案 1 :(得分:0)
您想要随机使用该模块。编码看起来像这样。
import random
name = input('What is your name? ')
print('Welcome to my quiz',name)
guess = 0
tries = 0
answer = 1
score = 0
num1=random.randint(1,100) #you can use whatever numbers you want here
num2=random.randint(1,100) #see above
answer=num1+num2
while guess != answer and tries < 2:
#print("What is",num1,"+",num2,"?") #you can use print or...
question="What is the sum of " + str(num1) +"+"+ str(num2)+"? "
guess=float(input(question)) #if you use print, remove the word question
if guess == answer:
print ("Correct")
score = score + 1
else:
print ("Incorrect")
score = score + 0
tries+=1
guess=0
tries = 0
num1=random.randint(1,100) #you can use whatever numbers you want here
num2=random.randint(1,100) #see above
answer=num1*num2
while guess != answer and tries <2:
#print("What is",num1,"*",num2,"?") #you can use print or...
question="What is the product of " + str(num1) +"*"+ str(num2)+"? "
guess=float(input(question)) #if you use print, remove the word question
if guess == answer:
print("Correct")
score = score + 1
else:
print("Incorrect")
score = score + 0
tries+=1
print("Thank you for playing",name,". You scored",score,"points")
我做的是创建了两个随机数(num1和num2),然后将它们相加/相乘。希望这有助于回答你的问题。