Python2.7中的程序
我需要帮助以Python2.7编写程序。 我的问题是,如果用户输入“是”,我不确定如何再次启动程序。这是我的节目:Python2.7中的程序,python,python-2.7,Python,Python 2.7,我需要帮助以Python2.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?"]
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!'
试试这个:
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"
也许你可以把你的程序放在一个循环中。如果输入是“否”,就打破循环
while(1):
your code
if reply == "no":
break
你好欢迎来到SO。您正在寻找循环。这个问题对任何人都没有帮助。浏览一些关于循环和函数的Python教程。非常感谢!!这有助于……)