else: roll_agn=input上的縮排僅在您退出while迴圈之後執行,但是while迴圈在執行else子句之前永遠不會結束,因此是無限迴圈。在
以下是乙個經過清理、結構更完善的版本:# assumes python 3.x
from random import randint
def get_int(prompt):
while true:
try:
return int(input(prompt)) # if python 2.x use raw_input instead of input
except valueerror:
# not an int
pass
def get_yn(prompt):
while true:
value = input(prompt).strip().lower() # if python 2.x use raw_input instead of input
if value in :
return true
elif value in :
return false
def roll(sides):
return randint(1, sides)
def main():
while true:
sides = get_int("number of sides on die (4, 6, or 12)? ")
if sides in :
print("you rolled a {}".format(roll(sides)))
else:
print("u no reed gud?")
if not get_yn("play again (y/n)? "):
print("thanks for playing!")
break
if __name__=="__main__":
main()
Python 編寫的分析網速程式
我們宿舍原來是 7 個人共享乙個 1m 的 adsl 寬頻,最近感覺這點頻寬用起來是越來越力不從心了,所以我們打算做一次全面的分析,看看到底是誰的哪種網路應用占用頻寬比較多。首先得測試一下大家在正常使用時的網速。還好我們的 tp link 的那個路由器有流量統計的功能,開啟這功能後路由器的管理頁面可...
python編寫定時關機程式
import os,sys,time from pyqt5 import qtcore,qtwidgets,qtgui class guanji object defpageshow self,page 設定視窗的位置和大小 page.setgeometry 400,400,400,200 設定視窗...
Python編寫備份程式指令碼
編寫backup.py指令碼,實現以下目標 1 需要支援完全和增量備份 2 周一執行完全備份 3 其他時間執行增量備份 4 備份檔案需要打包為tar檔案並使用gzip格式壓縮 import os import tarfile import hashlib import pickle from tim...