gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:42:28 26 4
gpt4 key购买 nike

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

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

我不知道我做错了什么,救命!!!!!!!!!!!!快!!!!!!!!!!!!!!!!!!

最佳答案

这应该可以完美工作

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 语句时,您正在重置 num1num2 现在它会重置每个循环

关于python - 我的 python 代码有什么问题? (乘法程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17178137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com