python怎么重复输出_如何根据输出在Python中重复函数?
你可以用while循環來實現這一點。只要沒有break或sys.exit,它就返回到start,這意味著這里的每一個錯誤輸入。希望這有幫助def read_the_file(output):
while True:
print """
Do you want me to read your newly created file?
Type [Y]es or [N]o
"""
question = raw_input("> ")
reading = output.read()
if question == 'yes'or question == 'Y' or question == 'y':
print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
break # or sys.exit
elif question == 'no' or question == 'N' or question == 'n':
sys.exit[1]
else :
print "wrong input"
read_the_file(output_file)
但我建議你把代碼改一下。現在,無論你想不想,每次文件被讀取時。你可以在用戶說“是”后再這樣做。如果使用with語句,則文件將只為以下未指定部分打開。文件在這里被讀取。def read_the_file(output):
while True:
print """
Do you want me to read your newly created file?
Type [Y]es or [N]o
"""
question = raw_input("> ")
if question == 'yes'or question == 'Y' or question == 'y':
# Open and read file here
with open(output, 'r') as f:
reading = f.read()
# File is now closed
print "BEGINNING OF FILE\n\n" + reading + "\n END OF FILE"
break # or sys.exit
elif question == 'no' or question == 'N' or question == 'n':
sys.exit[1]
else :
print "wrong input"
read_the_file(output_file)
總結
以上是生活随笔為你收集整理的python怎么重复输出_如何根据输出在Python中重复函数?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 家用笔记本电脑什么牌子好_南阳家用小型电
- 下一篇: android 卡片3d效果_小米发布伸