我很好奇为什么这个程序没有循环。该程序将运行一次,但在播放另一个游戏时,它不会记录获胜者并退出。
非常感谢任何帮助!
这是Python程序:
import random
random.seed()
print ("For each round please select from the following warriors: ")
print ("'C' or 'c' for Cowboy")
print ("'N' or 'n' for Ninja")
print ("'B' or 'b' for Bear")
print ("'Q' or 'q' for quit")
print()
gamesPlayed = 0
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
while not warriorStr.isalpha():
print()
print ("That's not a valid choice!")
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
if warriorStr == "q" and "Q":
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")
答案 0 :(得分:0)
将整个事物包裹起来。 尝试喜欢这个:
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
acceptables = ['a','c','n','q']
while warriorStr not 'q' and warriorStr in acceptables:
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")