简体   繁体   English

Python随机数学生成器失败

[英]Python random math generator fail

okay i have now encountered a problem with my code, when I run my code it will not give chance for the user to answer 好吧,我现在遇到了我的代码问题,当我运行我的代码时,它不会给用户机会

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")

i am unsure on how to fix this problem as i am new to programming. 我不确定如何解决该问题,因为我是编程新手。 it will not let the user guess and will just print incorrect 它不会让用户猜测,只会打印不正确

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

You are using int (num1 and num2) and string which comes from ops, fix it like so: 您正在使用来自ops的int(num1和num2)和字符串,请像这样修复它:

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

In Python you cannot add strings and ints (which is usually not what you want) Python is explicit, so you need to convert your numbers into strings explicitly. 在Python中,您无法添加字符串和整数(通常不是您想要的),Python是显式的,因此您需要将数字显式转换为字符串。 It is much more readable that way. 这样更易读。

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

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