目錄python
age = 18 inp_age = input('請輸入年齡>>>').strip() if inp_age.isdigit(): inp_age = int(inp_age) if age > inp_age: print('猜小了') elif age < inp_age: print('猜大了') else: print('猜對了') else: print('傻孩,年齡都輸很差')
age = 18 for i in range(3): inp_age = input('請輸入年齡>>>').strip() if inp_age.isdigit(): inp_age = int(inp_age) if age > inp_age: print('猜小了') elif age < inp_age: print('猜大了') else: print('猜對了') break else: print('傻孩,年齡都輸很差')
age = 18 count = 0 tag = True while tag: count += 1 inp_age = input('請輸入猜想年齡>>>').strip() if inp_age.isdigit(): inp_age = int(inp_age) if age > inp_age: print('猜小了') elif age < inp_age: print('猜大了') else: print('猜對了') break else: print('傻孩,年齡都輸很差') if count == 3: choice = input('是否繼續猜想,繼續請按Y or y ,任意鍵退出').strip().lower() if choice != 'y': tag = False else: continue
要求:git
import random #導入隨機庫 prize_dic = {0: '氣球', 1: '女友', 2: '勞斯萊斯', 3: '寶馬', 4: '牛逼', 5: '坦克', 6: '大炮', 7: '飛機'} # type:dict # 獎品單 user_price_dic = {} # type:dict age = random.randint(18,19) # 讓年齡隨機18或者19 count = 0 while count<3: count+=1 inp_age = input('請輸入猜想的年齡>>').strip() if not inp_age.isdigit(): print('輸入錯誤,請輸入數字') continue inp_age= int(inp_age) inp_age = int(inp_age) if age > inp_age: print('猜小了') elif age < inp_age: print('猜大了') else: print('猜對了') for k, v in prize_dic.items(): print(k, v) for i in range(2): choice_prize = input('請輸入獎品編號>>>').strip() if not choice_prize.isdigit(): print('撒掉,一邊彎曲') continue choice_prize = int(choice_prize) prize = prize_dic[choice_prize] print('得到了', prize) if prize not in user_price_dic: user_price_dic[prize] = 1 else: user_price_dic[prize] += 1 print('獎品以下', user_price_dic) break