繁体   English   中英

Python随机数学生成器失败

[英]Python random math generator fail

好吧,我现在遇到了我的代码问题,当我运行我的代码时,它不会给用户机会

print ("what is your username")
name = input () .title()
print (name, "welcome")
import random
score=0
question=0
for i in range(10):

 ops = ["+", "-", "*"]
num1 = random.randint (0,10)
num2 = random.randint (0,10)
oparator = random.choice(ops)
Q=(str(num1)+(oparator)+(str(num2)))
print (Q)


if oparator =='+':
    answer=num1+num2
    if Q == answer:
        print ("correct")
        score=score+1
    else:
         print ("incorrect")


if oparator =='-':
    answer=num1-num2
    if Q == answer:
        print ("correct")
        score=score+1
    else:
         print ("incorrect")


if oparator =='*':
    answer=num1*num2
    if Q == answer:
        print ("correct")
        score=score+1
    else:

        print ("incorrect")

我不确定如何解决该问题,因为我是编程新手。 它不会让用户猜测,只会打印不正确

what is your username
kurt
Kurt welcome
2*0
incorrect
>>> 

您正在使用来自ops的int(num1和num2)和字符串,请像这样修复它:

input (str(num1)+(ops)+str(num2)) 

在Python中,您无法添加字符串和整数(通常不是您想要的),Python是显式的,因此您需要将数字显式转换为字符串。 这样更易读。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM