2014-02-19 118 views
0

我試圖使用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!") 
+2

附註:我看不到任何實例在這個程序中你使用'str',需要做到這一點。你應該每次*調用'str'都要刪除*,因爲這會讓代碼混淆讀取。 – SethMMorton

回答

2
import random 


def newfunc(string1, string2): 
    if string1 == string2: 
     print "You selected",string1,"and the computer selected",string2,"you win!" 
    else: 
     print "You selected",string1,"and the computer selected",string2,"you lose!" 

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"] 
    # 
    while(True): 
     choice = raw_input("Ace, Diamond, or Spade? ") 
     computerChoice = random.choice(myList) 
     if choice not in myList: 
      print "That was not a valid choice." 
      break 
     newfunc(choice,computerChoice) 

AceDiamondSpade() 

這是基於diegoaguilar的回答,但它增加了新功能的前景。

我敢肯定,你只是複製/粘貼你的代碼直接到現場,但要小心你的縮進,如果可能,給予一定的間距在您的字裏行間,它使事情更容易閱讀。

+0

好工作@Matt :) – diegoaguilar

1

可以使用那種做而這蟒蛇在幾個不同的ways的結構:

所以恢復你的代碼應該是這樣的:

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"] 

    while True: 
     #Execute your game logic here 
     if input("Ace, Diamond, or Spade? ") not in myList: 
      break; 

while True你假設下面的代碼塊(遊戲)將繼續無限地執行。然而,在if條件你檢查結果輸入是否爲成員的list簡稱while循環將結束。

,我和評論表示贊同,你並不需要在你的代碼的任何點使用str。追求更多功能也會使它看起來更漂亮。