2017-03-18 45 views
-1

我需要Python中編寫我的程序的幫助2.7。 我的問題是,我不知道如何再次啓動程序,如果用戶輸入'是'。這裏是我的程序:Python 2.7中的程序

import random 

#Set up the lists for charades and the answers (words) 
charadelist = ["Mary's father has 5 daughters: Chacha, Chichi, Cheche, Chocho. What is the name of the 5th daughter?"]    
wordlist = ["Mary"] 

lencharades = len(charadelist) 

lenwords = len(wordlist) 

rndnum = random.randrange (0, lenwords) 

answer = wordlist[rndnum] 

charade = charadelist[rndnum] 

print "***Welcome to Charades!***" 
print "You are given a charade. Try to guess the answer:" 


print '"'+charade+'"' 
guess = raw_input('Your answer: ') 

if guess == answer: 
    print "Well done!" 
else: 
    print "Sorry, the correct answer is " + '"'+answer+'"' + '.' 

print 'Do you want to play again?' 
reply = raw_input('Type `yes` or `no`: ') 
if reply == 'yes': 

# How do I run the program again??? Please help 

if reply == 'no': 
    print 'Thanks for playing!' 
    exit 

謝謝。

+0

嗨!歡迎來到SO。你正在尋找循環。這個問題,對任何人都沒有幫助。通過循環和函數的一些Python教程。 – rll

回答

1

我會建議運行函數中你的遊戲,這樣你可以調用它的任何時間:

import random 

def runGame(): 
    #Set up the lists for charades and the answers (words) 
    charadelist = ["Mary's father has 5 daughters: Chacha, Chichi, Cheche, Chocho. What is the name of the 5th daughter?"]    
    wordlist = ["Mary"] 
    lencharades = len(charadelist) 
    lenwords = len(wordlist) 
    rndnum = random.randrange (0, lenwords) 
    answer = wordlist[rndnum] 
    charade = charadelist[rndnum] 
    print "***Welcome to Charades!***" 
    print "You are given a charade. Try to guess the answer:" 
    print '"'+charade+'"' 
    guess = raw_input('Your answer: ') 
    if guess == answer: 
     print "Well done!" 
    else: 
     print "Sorry, the correct answer is " + '"'+answer+'"' + '.' 


reply = "" 
while reply != 'no': 
    runGame() 
    print 'Do you want to play again?' 
    reply = raw_input('Type `yes` or `no`: ') 
    if reply == 'no': 
     print 'Thanks for playing!' 
+0

非常感謝你!這有幫助。 :) – Liana

0

試試這個:

import random 


    #Set up the lists for charades and the answers (words) 
    charadelist = ["Mary's father has 5 daughters: Chacha, Chichi, Cheche, Chocho. What is the name of the 5th daughter?"]    
    wordlist = ["Mary"] 

    lencharades = len(charadelist) 

    lenwords = len(wordlist) 

    rndnum = random.randrange (0, lenwords) 

    answer = wordlist[rndnum] 

    charade = charadelist[rndnum] 

    print "***Welcome to Charades!***" 
    print "You are given a charade. Try to guess the answer:" 

    rep = "yes" 
    while rep == "yes": 
     print '"'+charade+'"' 
     guess = raw_input('Your answer: ') 

     if guess == answer: 
      print "Well done!" 
     else: 
      print "Sorry, the correct answer is " + '"'+answer+'"' + '.' 

     print 'Do you want to play again?' 
     reply = raw_input('Type `yes` or `no`: ') 
     if reply == 'yes': 
     pass 

     # How do I run the program again??? Please help 

     if reply == 'no': 
      print 'Thanks for playing!' 
      rep = "no" 
0

也許你可以把你的程序在一個循環。如果輸入是「否」,請打開循環。

while(1): 
    your code 
    if reply == "no": 
     break