2017-03-18 46 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