我不确定我做了什么,但现在当我玩游戏时,它在我输入我的选择(r,p或s)后结束 它与验证有关吗?
这是代码:
from random import randint
computer = randint(1,3)
r = "r"
p = "p"
s = "s"
print ("The computer has chosen. Your turn")
player = input ("r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
a = 1
while (a == 1):
if (player not in(r,p,s)):
player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player in (r,p,s)):
a = 2
if (computer == 1):
AI = ("rock")
if (computer == 2):
AI = ("paper")
if (computer == 3):
AI = ("scissors")
if (player == r and computer == 1):
print ("lol draw")
if (player == p and computer == 2):
print ("lol draw")
if (player == s and computer == 3):
print ("lol draw")
if (player == r and computer == 3):
print ("You WIN!!!!!! AI chose " + AI)
if (player == p and computer == 1):
print ("You WIN!!!!!! AI chose " + AI)
if (player == s and computer == 2):
print ("You WIN!!!!!! AI chose " + AI)
if (player == s and computer == 1):
print ("You LOSE!!!!!! AI chose " + AI)
if (player == r and computer == 2):
print ("You LOSE!!!!!! AI chose " + AI)
if (player == p and computer == 3):
print ("You LOSE!!!!!! AI chose " + AI)
答案 0 :(得分:1)
如果输入r,p或s,则输入无限循环。
a = 1
while (a == 1): #always enters
if (player not in(r,p,s)): #if it is r or p or s it skips this
player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player in (r,p,s)):
a = 2
#and loops back, a being still 1
你正在寻找这样的东西:
player = None
while player not in (r, p, s):
player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
答案 1 :(得分:1)
清理此代码可以做很多事情,但问题来自于此:
while (a == 1):
if (player not in(r,p,s)):
player = input ("That wasn't r, p, or s. Please try again. r is Rock, p is Paper, and s is Scissors. Put your letter in HERE-----> ")
if (player in (r,p,s)): #this shouldn't be indented this far
a = 2 #this one either, obviously :)
答案 2 :(得分:1)
现在你的程序陷入12-16行的无限循环中。你应该弄清楚为什么你自己,因为你会从中学到更多。
在样式注释中,使用过多的if语句使程序难以阅读。尝试找出一种更简洁的方法来回答问题而不必列举每个案例。