python學習日記(簡易練習)
阿新 • • 發佈:2018-11-19
#簡易計算器,蠢新一枚,功能尚不完善,本為個人練習,僅供參考 while 1: a = input('請輸入第一個運算數:').strip()#可輸入前後帶空格的數字 if a.lower() == 'exit':#不區分大小寫的實現 break c = input('請輸入運算子(僅限+,-,*,/,**):').strip() if c.lower() == 'exit': break b = input('請輸入第二個運算數:').strip() if b.lower() == 'exit':break if c == '+': s = float(a) + float(b) print(s) elif c == '-': s = float(a) - float(b) print(s) elif c == '*': s = float(a) * float(b) print(s) elif c == '/': if float(b) != 0: s = float(a) / float(b)print(s) else: print('除數不可為零!') continue elif c == '**': s = float(a) ** float(b) print(s) else: print('運算子有誤!請輸入"+,-,*,/,**"') continue cont = input('任意鍵繼續,退出請輸入N(不區分大小寫):') if cont.lower() == 'n': breakelse: continue
#輸出輸入字串的元素型別個數,其他方法同理,isalph(),isalnum() s = input('請輸入:') count = 0 for i in s: if i.isdigit(): count += 1 print(count)
員工資訊列表,可持續插入新的資訊,輸入提示符退出
employee = ['a','f','d','s'] while 1: newem = input('請輸入新職工姓名(退出請輸入exit(case-insensitive)):') if newem.lower() == 'exit': break else: employee.append(newem) print(employee) print('新增成功')
賦值運算:
# Fibonacci series: 斐波納契數列 # 兩個元素的總和確定了下一個數 a, b = 0, 1 while b < 10: print(b,end=' ') a, b = b, a+b
這種輔助運算的要點是:先計算等號右邊的,再賦值。
首先,a = 0, b = 1.
然後,下一步,先等號右邊,b 當前為1, a+b = 0 + 1 = 1,
再賦值:b賦值給a ,a = b(b=1) ,a = a + b (a+b=0+1=1)
現在,a = 1,b = 1這次迴圈結束。
pass