我試圖使用while循環,使這場比賽將繼續播放,除非用戶輸入一個字符串,它是不是在myList中的選擇之一。我將在哪裏放置這個while循環? (對不起,我對編碼比較陌生)。在Rock,Paper,Scissors Type遊戲中使用while循環。 (蟒蛇)
import random
def AceDiamondSpade():
#Set up a list which contains Ace, Diamond and Spade so that the computer can randomly choose one of them.
myList = ["Ace", "Diamond", "Spade"]
#
choice = input("Ace, Diamond, or Spade? ")
computerChoice = random.choice(myList)
playAgain = True
if str(choice) != str(myList[0]) and str(choice) != str(myList[1]) and str(choice) != str(myList[2]):
playAgain = False
print ("invalid! Please enter again or make sure that you have capitalized the first letter of your answer!")
if str(choice) == str(computerChoice):
print ("You and the computer selected the same thing, you tie!")
if str(choice) == "Ace" and str(computerChoice) == "Diamond":
print ("You selected Ace, and the comptuer selected Diamond, you win!")
if str(choice) == "Diamond" and str(computerChoice) == "Spade":
print ("You selected Diamond, and the Computer selected Spade, you win!")
if str(choice) == "Spade" and str(computerChoice) == "Ace":
print ("You selected Spade, and the comptuer selected Ace, you win!")
if str(choice) == "Ace" and str(computerChoice) == "Spade":
print ("You selected Ace, and the computer selected Spade, you lose!")
if str(choice) == "Spade" and str(computerChoice) == "Diamond":
print ("You selected Spade, and the computer selected Diamond, you lose!")
if str(choice) == "Diamond" and str(computerChoice) == "Ace":
print ("You selected Diamond, and the computer selected Ace, you lose!")
附註:我看不到任何實例在這個程序中你使用'str',需要做到這一點。你應該每次*調用'str'都要刪除*,因爲這會讓代碼混淆讀取。 – SethMMorton