2017-03-18 1 views
-1

내 프로그램을 파이썬으로 작성하는 데 도움이 필요합니다. 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 

고마워요.

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

안녕하세요! 에 오신 것을 환영합니다. 당신은 루프를 찾고 있습니다. 이 질문은 누구에게나 도움이되지 않습니다. 루프와 함수에 대한 파이썬 자습서를 살펴보십시오. – rll

답변

1

나는, 함수 내에서 게임을 실행하면 언제든지 호출 할 수있는 방법을 추천 할 것입니다. 입력이 "아니오"이면 루프를 종료하십시오.

while(1): 
    your code 
    if reply == "no": 
     break 
+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

은 어쩌면 당신은 루프에서 프로그램을 넣을 수 있습니다

관련 문제