1

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

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 

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

4

4 回答 4

1

您使用了两次选择,所以虽然您的要求可能是这样的:5 * 6
您的 if 语句可以检查类似的内容:11 * 4
您还在将 int 与字符串进行比较,因此在条件中将它们转换为 int也是。

此外,choice() 是一个不好的函数,如果您使用 randrange() 代替,则根本不必创建列表。尝试将您的初始choice() 返回值都分配给变量,并稍后在提示和条件中使用这些值。

于 2013-06-18T20:31:52.967 回答
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而不是选择,因为那么你不需要一个你很接近的列表!

并且您正在重置num1num2当您使用 if 语句时,它会重置每个循环

于 2013-06-18T20:49:18.537 回答
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 
于 2013-06-18T20:24:22.840 回答
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!" 

谢谢你们!!!!你们都帮忙了!!!(对不起克里斯蒂安,我只是复制错了......)

于 2013-06-18T22:05:53.870 回答