我正在做一个程序来做一个猜谜游戏;我需要能够让程序给出H或L的反馈,只有当一个数字比随机数高3或低3的时候。你知道吗
这就是我现在拥有的
import random
def game3():
rndnumber = str(random.randint(0,9999)) #gets a number between 0-9999
while len(rndnumber) < 4:
rndnumber = '0'+ rndnumber # adds 0s incase the number is less then a 1000
print(rndnumber) #lets me know that the program generates the right type of number (remove this after testing)
feedback = 0 #adds a variable
for x in range(1,11): #makes a loop that runs for 10 times
print("Attempt",x)
attempt = input("Guess a number between 0-9999:")#gets the users guess
feedback = "" #makes a feedback variable
for y in range(4): #makes a loop that runs for 4 times
if attempt[y] == rndnumber[y]: #if attempt is the same then add a Y to the number
feedback += "Y"
elif attempt[y] < rndnumber[y]:
feedback += "L"
elif attempt[y] > rndnumber[y]:
feedback += "H"
else:
feedback += "N"
print(feedback)
if x == 10:
print("You Lose the correct answer was",rndnumber)
if feedback == "YYYY" and x > 1:
print("You win it took",x,"attempts.")
break; #stops the program
elif feedback == "YYYY":
print("You won on your first attempt!")
break; #stops the program
资料来源:
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html
在第10行中,您在变量尝试中保存了一个字符串。 然而,在第13行中,您将尝试用作字典。你知道吗
你可能想重新考虑一下你的整个方法。你知道吗
编辑:
当然,我也曾经做过一个猜谜游戏。虽然我现在肯定会使用一种不同的方法,但我认为它可能会对您有所帮助,并在python3代码中为您自己的游戏构建需求。你知道吗
你可以用
相关问题 更多 >
编程相关推荐