简体   繁体   English

我将如何分配运算符列表,以便计算出随机数以告诉用户它们是否正确?

[英]How would I assign the list of operators so that the random numbers are worked out to tell the user if they're correct or not?

How would I assign the list of operators so that the random numbers are worked out to tell the user if they're correct or not? 我将如何分配运算符列表,以便计算出随机数以告诉用户它们是否正确?

    # Controlled Assessment - Basic Times Table Test
import random

score = 0

print ("Welcome to the times table test")

name = input("Please type your name: ")


print ("How to play")
print ("Step 1: When you see a question work out the answer and type it in the space.")
print ("Step 2: Once you have typed your answer press the enter key.")
print ("Step 3: The program will tell you if you're right or wrong.")
print ("Step 4: The next question will load and you can repeat from step 1.")
print ("When you have answered all 10 questions your final score will be    printed.")




for q in range(10):
    Number1 = random.randint(1,12)
    Number2 = random.randint(1,12)
    ListOfOperator = ['+','-','*']
    Operator =random.choice(ListOfOperator)
    print ('what is' ,Number1,Operator,Number2)
    Answer= input ("Please Type Your Answer: ")

realanswer = (Number1,Operator,Number2)

if ListOfOperator:
    ListOfOperator=['+'] = Number1+Number2
    ListOfOperator=['-'] = Number1-Number2
    ListOfOperator=['*'] = Number1*Number2




if Answer==realanswer:
    print("Your answer is correct")
    score = score + 1
    print (score)
else:
    print("Your answer is incorrect, the correct answer is.",realanswer,".")
    print (score)

The code that needs to assign to the list of operators is... 需要分配给运算符列表的代码是...

    if ListOfOperator:
    ListOfOperator=['+'] = Number1+Number2
    ListOfOperator=['-'] = Number1-Number2
    ListOfOperator=['*'] = Number1*Number2

It should work out the answer to each question using the function I'm telling the program that if the operator from the operator list is * to work out Number1*Number2 它应该使用我告诉程序的功能算出每个问题的答案,即如果算子列表中的算子是*,则算出Number1 * Number2

The current output for telling them if the answer is correct or not prints 告诉他们答案是否正确的当前输出

Your answer is incorrect, the correct answer is Number1*Number2. 您的答案不正确,正确的答案是Number1 * Number2。

when if the question is what is 10*3 it should be printing 当问题是10 * 3是多少时,它应该打印

Your answer is incorrect, the correct answer is 30. 您的答案不正确,正确的答案是30。


Now that I have this code... 现在我有了这段代码...

if Operator == '+':
    realanswer = Number1+Number2
elif Operator == '-':
    realanswer = Number1-Number2
elif Operator == '*':
    realanswer = Number1*Number2




if Answer==realanswer:
    print("Your answer is correct")
    score = score + 1
    print (score)
else:
    print("Your answer is incorrect, the correct answer is.",realanswer,".")
    print (score)

The program always prints that the question is incorrect even with the correct answer inputted, it will then print the correct answer, how would I make it so that It would tell them if it's correct too? 即使输入了正确的答案,程序也会始终打印出问题是不正确的,然后它将打印正确的答案,我该怎么做,以便它也可以告诉他们是否正确?

The operator module implements basic operations as functions. operator模块将基本操作实现为功能。 Define a dict that maps operator symbols such as "+" to the operator function then use that map to do the calculation. 定义一个将操作符(例如"+"映射到操作符的dict ,然后使用该映射进行计算。

import random
import operator

op_map = {'+':operator.add, '-':operator.sub, '*':operator.mul}
op_list = list(op_map.keys())

score = 0

print ("Welcome to the times table test")

name = input("Please type your name: ")

print ("How to play")
print ("Step 1: When you see a question work out the answer and type it in the space.")
print ("Step 2: Once you have typed your answer press the enter key.")
print ("Step 3: The program will tell you if you're right or wrong.")
print ("Step 4: The next question will load and you can repeat from step 1.")
print ("When you have answered all 10 questions your final score will be    printed.")

for q in range(10):
    Number1 = random.randint(1,12)
    Number2 = random.randint(1,12)
    Operator =random.choice(op_list)
    print ('what is' ,Number1,Operator,Number2)
    while True:
        try:
            Answer= int(input("Please Type Your Answer: "))
            break
        except ValueError:
            print("Must be an integer... try again...")

    realanswer = op_map[Operator](Number1, Number2)

    if Answer==realanswer:
        print("Your answer is correct")
        score = score + 1
        print (score)
    else:
        print("Your answer is incorrect, the correct answer is.",realanswer,".")
        print (score)

To perform multiple check like this you can use if, elif statements: 要执行这样的多次检查,可以使用if,elif语句:

if Operator == '+':
    realanswer = Number1+Number2
elif Operator == '-':
    realanswer = Number1-Number2
elif Operator == '*':
    realanswer = Number1*Number2

For your reference: Python Docs 供参考: Python Docs

...

def realanswer(Num1, Op, Num2):
    return {
        '+': Num1 + Num2,
        '-': Num1 - Num2,
        '*': Num1 * Num2,
    }[Op]

for q in range(2):
    Number1 = random.randint(1,12)
    Number2 = random.randint(1,12)
    ListOfOperator = ['+','-','*']
    Operator =random.choice(ListOfOperator)
    print ('what is',Number1,Operator,Number2)
    userInput = input("Please Type Your Answer: ")
    Answer = 0
    try:
        Answer = int(userInput)
    except ValueError:
        print("Input not convertible to int!")
    rAnswer = realanswer(Number1,Operator,Number2)
    if Answer == rAnswer:
        print("Correct!")
    else:
        print("Incorrect...")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何实现random.sample,以便它在不重复的情况下从3个变量中选择2个? - How would i implement random.sample so that it would choose 2 out of 3 variables without it repeating? 如何生成随机数列表,使它们的总和等于随机选择的数 - How to generate a list of random numbers so their sum would be equal to a randomly chosen number 我如何使用函数将两个 arrays 洗牌,以便打印出随机卡 - How can i shuffle the two arrays using functions so that it would print out a random card 如何生成随机数并将其分配给python中的变量? - How can I generate random numbers and assign them to variables in python? 如何从随机数字列表中获取最大值 - How to get the max value out of a random list of numbers Python 我如何让它在 Python 中从 1 和 2 中选择一个随机数? - How would I get it to choose a random number out of 1 and 2 in Python? 如何对列表中的随机数样本进行编码,以便根据前面的数字生成的每个数字都遵循某些条件? - How can I code a sample of random numbers from a list, so that each number generated follows some condition depending on the preceding number? 我将如何 select 文件中的随机单词供用户解读? - How would I select a random word from a file for a user to unscramble? Python 开始:无法弄清楚如何告诉用户他们的数字是奇数还是偶数(5 个数字) - Python Beginning: Can't figure out how to tell user their number is odd or even (for 5 numbers) 如何分配此变量? - How would I assign this variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM