我的python代码有什么问题? (乘法程序)

时间:2013-06-18 20:19:29

标签: python multiplication

所以我尝试制作一个乘法游戏。所以它有点有用,但是当我输入正确的答案时,它会输入两次“错误”......以及如果我输错了答案。

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 

我不知道我做错了什么帮助!!!!!!!!!!!!! QUICK !!!!!!!!!!!!!!!!!

4 个答案:

答案 0 :(得分:1)

你正在使用选择两次,所以你问的问题可能是这样的:5 * 6
您的if语句可以检查类似的内容:11 * 4
您还将int与string进行比较,因此也将条件转换为带有条件的int。

另外,如果你使用了randrange()而没有创建一个列表,那么choice()对此来说是一个不好的函数。尝试将两个初始choice()返回值分配给变量,并在稍后的提示和条件中使用它们。

答案 1 :(得分:1)

这应该可以完美地运作

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代替选择原因然后你不需要你关闭的列表!

当你使用ifre if语句时,你正在重置num1num2现在它会重置每个循环

答案 2 :(得分:0)

而不是

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 

答案 3 :(得分:0)

对此问题的回答是>>>>>>回答CHRISTIAN CAREAGA !!!!!

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

谢谢大家!!!!你们全都帮了!!! (对不起克里斯蒂安,我刚刚复制错了......)