2014-02-19 52 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。追求更多功能也会使它看起来更漂亮。