这里我们通过书写一个小游戏,来熟悉下python中的 if 和 for
游戏名称:猜年龄(Guess Age)
1 #定义一个年龄数字范围(1-80) 2 age = 25 3 counter = 0 4 5 #使用for循环和if判断来书写游戏 6 for i in range(10): 7 if counter < 3: #用户有3次猜测数字机会 8 guess_num = int(input("input you guess num(1-80):")) 9 if guess_num == age:10 print("Congratulations! you got it.")11 break #跳出循环12 elif guess_num > age: #如果猜测年龄大于定义年龄13 print("Think Smaller!")14 else: #否则15 print("Think Big!")16 else: 17 contunue_confirm = input("Do you want to continue because you are stupid(Y/N):") 18 if contunue_confirm == 'y' :19 counter = 020 continue21 else:22 print("Bye...")23 break24 counter += 1 #counter = counter +1