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

こんにちは!ようこそ。あなたはループを探しています。この質問は、誰にとっても役立たない。ループと関数に関するいくつかのPythonチュートリアルを見てください。 – rll

答えて

1

私は、関数内でゲームを実行しているあなたはいつでもそれを呼び出すことができる方法をお勧めします:たぶん、あなたはループの中で、あなたのプログラムを置くことができる

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

ありがとうございました!これは助けになりました。 :) – Liana

0

はこれを試してみてください。入力が「いいえ」の場合は、ループを解除します。

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

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!'