我目前正在编写一个关于Python上的货币转换器的程序,我在最后阶段需要帮助我问一个问题"你想再转换一次"如果答案以" n"开头然后我写了一条消息,程序退出。问题是我希望程序重新启动,如果用户想再次转换"。我需要定义如何重新启动程序,但我不确定如何。最后,如果用户写了除“" y"或" n"非常感谢帮助。到目前为止它看起来像这样:
def restart_programme
answer=str(input('Do you want to convert again?'))
if answer.lower().startswith("n"):
print("Thank you for using Currency Converter today,Goodbye "+name)
exit()
if answer.lower().startswith("y") or ("Y"):
restart_programme()
答案 0 :(得分:0)
将整个程序包含在循环中。
answer = 'y'
while answer.lower().startswith("y"):
#Whatever you want to do
answer=str(input('Do you want to convert again?'))
if answer.lower().startswith("n"):
print("Thank you for using Currency Converter today,Goodbye "+name)
else:
#The anything else part
OR
将整个程序包含在一个函数中,然后再次调用该函数。
def restart_programme():
#Whatever you want to do
answer=str(input('Do you want to convert again?'))
if answer.lower().startswith("n"):
print("Thank you for using Currency Converter today,Goodbye "+name)
exit()
if answer.lower().startswith("y"):
restart_programme()
restart_programme()
答案 1 :(得分:0)
python 2.7
def restart_programme():
answer = raw_input('Do you want to convert again? [Y/n]\n')
if answer.lower().startswith('n'):
print('Thank you for using Currency Converter today, Goodbye ' + name)
quit()
elif answer.lower().startswith('y'):
restart_programme()
print ('Debug: Program will now restart\n\n')
else:
print('You didnt choose a valid option')
raw_input('Press Enter to Restart\n')
restart_programme()
答案 2 :(得分:0)
真的没有人在测试过他们的代码之前回答过吗?两者都存在同样的问题。您应该阅读this
def got_milk():
answer = False
while not answer:
milk = raw_input('Got Milk?')
if not milk in ['N', 'No', 'n', 'no']:
print 'Thanks'
answer = True
这可能是您期望做的某些版本:
def restart_programme():
check = True
while check:
answer=raw_input('Do you want to convert again?').lower()
if answer.startswith("n"):
print("Thank you for using Currency Converter today, Goodbye")
check = False
exit()
if answer.startswith("y") or answer.startswith("Y"):
print "Let's have some more fun."