2014-11-15 2 views
-1
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

У вас есть ошибка отступов в вашем вставленном коде. –

+0

Пожалуйста, пометьте его как python-3, если это вопрос Python v3.0 – hjpotter92

ответ

2
>>> import random 
>>> random.randint(0,10) 
3 
>>> random.randint(0,10) 
8 
>>> random.randint(0,10) 
10 

Вы можете использовать питон 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 метод, и он будет генерировать уникальные значения для вас каждый раз, когда вы называете его.

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), а затем добавил/умножил их вместе. Надеюсь, это поможет ответить на ваш вопрос.