[英]What is wrong with my python code? (Multiplication program)
所以我试图做一个乘法游戏。 这样就可以了,但是当我输入正确的答案时,它会两次输入“错误” ...以及输入错误的答案。
import sys #imports sys
random1=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes a list with numbers 1-12
random2=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes another list with numbers 1-12
from random import choice #gets a random number
while True: #makes the following code run in a loop
print "To exit this game type 'exit'" #prints stuff
theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff.
if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
print "Now exiting game!" #prints stuff
sys.exit() #exits
elif theyputinstuffhere == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!)
print "Correct!" #print stuff
else:
print "Wrong!" #otherwise print stuff
我不知道我做错了什么! 快!!!!!!!!!!!!!!!!!
您两次使用选择,所以您要问的可能是这样的:5 * 6
您的if语句可以检查类似:11 * 4
您还要将int与string进行比较,因此也要在有条件的情况下将它们转换为int。
另外,如果您使用了randrange(),则choice()对此功能来说是一个不好的函数,您根本不必创建列表。 尝试将两个初始choice()返回值都分配给变量,然后在提示和条件条件下使用它们。
这应该工作完美
import sys
import random
while True:
num1 = random.randint(0,12)
num2 = random.randint(0,12)
print "To exit this game type 'exit'"
theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ")
if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
print "Now exiting game!" #prints stuff
sys.exit() #exits
elif int(theyputinstuffhere) == num1*num2:
print "Correct!" #print stuff
else:
print "Wrong!"
使用random.randint
而不是选择原因,那么您不需要关闭的列表!
当您使用if语句时,您正在重置num1
和num2
,它现在重置每个循环
代替
theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff.
if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
print "Now exiting game!" #prints stuff
sys.exit() #exits
elif int(theyputinstuffhere) == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!)
print "Correct!" #print stuff
else:
print "Wrong!" #otherwise print stuff
做这个:
num1 = choice(random1)
num2 = choice(random2)
theyputinstuffhere = raw_input("What is " + num1 + " times " + num2 + "? ") #asks the user things like 1*5 or some other random stuff.
if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
print "Now exiting game!" #prints stuff
sys.exit() #exits
elif theyputinstuffhere == int(num1)*int(num2): #if what i typed is right, print stuff (I think i messed up here!)
print "Correct!" #print stuff
else:
print "Wrong!" #otherwise print stuff
这个问题的答案是>>>>>>克里斯蒂安·卡雷亚加回答了!
import sys
import random
while True:
num1 = random.randint(0,12)
num2 = random.randint(0,12)
print "To exit this game type 'exit'"
theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ")
if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game
print "Now exiting game!" #prints stuff
sys.exit() #exits
elif int(theyputinstuffhere) == num1*num2:
print "Correct!" #print stuff
else:
print "Wrong!"
谢谢你们!!!! 大家都来了!!! (抱歉,克里斯蒂安,我只是错了...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.