『壹』 python 做一個循環 要求輸入大於0的數字 判斷其不為負數或者字母
defgetInt(prompt,limit=(0,None)):
whileTrue:
try:
x=int(input(prompt))
iflimit[0]isnotNoneandx<limit[0]:
continue
iflimit[1]isnotNoneandlimit[1]<x:
continue
returnx
except:
pass
defsetlimits():
lb=getInt('PleaseenteraLowbound:',(1,None))
hb=getInt('PleaseenteraHighbound:',(lb,9999))
return(lb,hb)
lb,hb=setlimits()
num=getInt('Pleaseenteranumberbetween%dand%d'%(lb,hb),
limit=(lb,hb))
『貳』 python 做一個循環 要求輸入大於0的數字 判斷其不為負數或者字母
def getInt(prompt, limit=(0, None)):
while True:
try:
x = int(input(prompt))
if limit[0] is not None and x < limit[0]:
continue
if limit[1] is not None and limit[1] < x:
continue
return x
except:
pass
def setlimits():
lb = getInt('Please enter a Low bound: ', (1, None))
hb = getInt('Please enter a High bound: ', (lb, 9999))
return (lb, hb)
lb, hb = setlimits()
num = getInt('Please enter a number between %d and %d' % (lb, hb),
limit=(lb, hb))
『叄』 python中x % n>0是什麼意思
x%n >0這坦顫帆個語句洞塵一般是用在條件分支中:
例如:
循環列表x變數讓雹x是0~10的數字。
如果(if)變數(x)除於(%)2所余的數 大於(>) 0:#執行以下內容:
print(x) #列印變數x
得到的結果是:0~10 中的奇數 1,3,5,7,9.