什么';我的python代码中有什么错误?i';我无法获得任何输出。?
我无法理解代码中的错误,为什么没有得到任何输出?什么';我的python代码中有什么错误?i';我无法获得任何输出。?,python,Python,我无法理解代码中的错误,为什么没有得到任何输出? 我已经彻底检查了我的代码,但仍然找不到任何错误。 当我试着在pycharm上运行它时,它什么也没做 import random def ques(): name=input("What's your name?:") print("Hi",name,"!") choice=random.choice("+x&qu
我已经彻底检查了我的代码,但仍然找不到任何错误。 当我试着在pycharm上运行它时,它什么也没做
import random
def ques():
name=input("What's your name?:")
print("Hi",name,"!")
choice=random.choice("+x")
finish=False
quesno=0
correctques=0
while finish == False:
if quesno<10|questno>=0:
no1=random.randrange(1,10)
no2=random.randrange(1,10)
print((no1),(choice),(no2))
ans=int(input("What's the answer?:"))
quesno=quesno+1
if choice==("+"):
realans=no1+no2
if ans==realans:
print("That's the correct answer!")
correctques=correctques+1
else:
print("That's incorrect!,the answer was",realans,"!")
if choice==("x"):
realans=no1*no2
if ans==realans:
print("That's the correct answer!")
correctques=correctques+1
else:
print("That's incorrect!,the answer was",realans,"!")
elif choice==("-"):
realans=no1-no2
if ans==realans:
print("That's the correct answer")
correctques=correctques+1
else:
print("That's incorrect!,the answer was",realans,"!")
else:
finish=True
else:
print("Good Job",name,"! You have finished the quiz")
随机导入
def ques():
name=输入(“你叫什么名字?:”)
打印(“你好,姓名,!”)
选择=随机。选择(“+x”)
完成=错误
quesno=0
修正值=0
当finish==False时:
如果quesno=0:
no1=随机。随机范围(1,10)
no2=随机。随机范围(1,10)
打印((第1)、(选择)、(第2))
ans=int(输入(“答案是什么:”)
quesno=quesno+1
如果选项==(“+”):
realans=no1+no2
如果ans==realans:
打印(“这是正确的答案!”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
如果选项==(“x”):
realans=no1*no2
如果ans==realans:
打印(“这是正确的答案!”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
elif选项==(“-”):
realans=no1-no2
如果ans==realans:
打印(“这是正确的答案”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
其他:
完成=真
其他:
打印(“干得好”,姓名,“!你已经完成了测验”)
您需要调用该函数。
在代码末尾,只需键入ques(),这将调用您的函数,您应该能够获得输出。您运行了它吗?将此添加到代码末尾
ques()
如果名称=“\uuuuu main\uuuuuuuu”:
ques()
随机导入
def ques():
name=输入(“你叫什么名字?:”)
打印(“你好,姓名,!”)
选择=随机。选择(“+x”)
完成=错误
quesno=0
修正值=0
当finish==False时:
如果quesno<10 | quesno>=0:
no1=随机。随机范围(1,10)
no2=随机。随机范围(1,10)
打印((第1)、(选择)、(第2))
ans=int(输入(“答案是什么:”)
quesno=quesno+1
如果选项==(“+”):
realans=no1+no2
如果ans==realans:
打印(“这是正确的答案!”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
完成=真
如果选项==(“x”):
realans=no1*no2
如果ans==realans:
打印(“这是正确的答案!”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
完成=真
elif选项==(“-”):
realans=no1-no2
如果ans==realans:
打印(“这是正确的答案”)
correctques=correctques+1
其他:
打印(“那是不正确的!答案是“realans”!”)
完成=真
其他:
完成=真
其他:
打印(“干得好”,姓名,“!你已经完成了测验”)
ques()
在第12行,您应该使用或而不是
。(|
是一个按位OR运算符,这可能不是您打算使用的)@Edric您是指还是如果quesno=0:
同样,我假设questno
应该是quesno
。在这种情况下,如果0,您甚至可以执行,如果quesno=0,您是否调用过ques
?如果quesno=0,则应该是<代码>|
是一个。您得到任何输出吗?比如,您是否收到您叫什么名字?
提示?另外,您的代码中有ques()
,对吗?你必须实际调用函数来运行它,你不能只是定义它并期望它被执行。是的,这只是一个位运算符错误,现在它工作正常了,谢谢:)是的,这只是一个位运算符错误,现在它工作正常了,谢谢:)我也测试了你的代码,我认为如果你在他要求你乘法时输入错误,当你出错时,它会无限地打印输出。如果我是正确的,试着找出错误(我可能弄错了)是的,我刚刚检查过,如果一开始没有给出正确的答案,它确实是无限期打印的。我发现了错误。基本上,在打印(“这是不正确的”)之后,您需要将finish设置为true,因为它只打印并保留在循环中。当你输入错误时,它应该打印出你犯了错误,然后设置finisht==true,然后当循环再次出现时,它将是finish==true,它将中断循环。你能告诉我在哪一行吗?
import random
def ques():
name = input("What's your name?:")
print("Hi", name, "!")
choice = random.choice("+x")
finish = False
quesno = 0
correctques = 0
while finish == False:
if quesno < 10 | quesno >= 0:
no1 = random.randrange(1, 10)
no2 = random.randrange(1, 10)
print((no1), (choice), (no2))
ans = int(input("What's the answer?:"))
quesno = quesno + 1
if choice == ("+"):
realans = no1 + no2
if ans == realans:
print("That's the correct answer!")
correctques = correctques + 1
else:
print("That's incorrect!,the answer was", realans, "!")
finish = True
if choice == ("x"):
realans = no1 * no2
if ans == realans:
print("That's the correct answer!")
correctques = correctques + 1
else:
print("That's incorrect!,the answer was", realans, "!")
finish = True
elif choice == ("-"):
realans = no1 - no2
if ans == realans:
print("That's the correct answer")
correctques = correctques + 1
else:
print("That's incorrect!,the answer was", realans, "!")
finish = True
else:
finish = True
else:
print("Good Job", name, "! You have finished the quiz")
ques()