程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python必考的5道面試題集合

編輯:Python

1、使用while循環實現輸出2 - 3 + 4 - 5 + 6 ... + 100的和#方法一#從2開始計算i = 2#定義一個變量用於保存結果sum=0while i <= 100: if i % 2 == 0: sum = sum + i else: sum = sum - i i += 1print("2-3+4-5+6...+100=",sum)#方法二n=3sum=2while n<=100 : #n對2取余 if n % 2 != 0: sum = sum - n n = n + 1 else: sum = sum + n n = n + 1print("2-3+4-5+6...+100=",sum)

運行結果:

2、從鍵盤獲取一個數字,然後計算它的階乘,例如輸入的是3,那麼即計算3!的結果,並輸出。

提示:

1!等於1

2!等於1*2

3!等於1*2*3

n!等於1*2*3*...*n

n = int(input("請輸入一個非負的數字:")) # 負數不算階乘def factorial(n): if n == 0: return 1 # 0的階乘是1 else: return n * factorial(n - 1)if __name__ == '__main__': result=factorial(n) print("{}的階乘為:{}".format(n,result))

3、用戶輸入考試成績,當分數高於90(包含90)時打印A;否則如果分數高於80(包含80)時打印B;否則如果當分數高於70(包含)時打印C;否則如果當分數高於60(包含60)時打印D;其他情況就打印E。try: score=float(input('請輸入考試成績:')) if score>=90: print('A') elif 80<=score<90: print('B') elif 70<=score<80: print('C') elif 60<=score<70: print('D') else: print('E')except Exception as e: print('您輸入有誤!')

4、假設一年的定期利率為3.52%,需要幾年才能讓定期存款連本帶息的翻一番(例如:需要多少年10000才能變成20000)?save_money = float(input("請輸入你要存入銀行的錢:"))print("你存了{}元到銀行".format(save_money))total_money = save_money * 2 # 定義變量用於保存總錢數year = 1 # 定義變量用於記錄年份while save_money < total_money: save_money *= (1 + 0.0352) year += 1print("定期利率為3.52%,需要{}年本金和利息才能翻一番!".format(year))

5、將列表a =["I","T","e","s","t","e","r"]拼接成字符串,請用多種方法實現。# 方法一 字符串函數調用a = ["I","T","e","s","t","e","r"]print("".join(a))#方法二 for循環a = ["I","T","e","s","t","e","r"]s = ""for item in a: s += itemprint(s)



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved