Python 猜数字游戏

2019-01-08 08:23:58来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

分别用while循环和for循环来玩转猜数字游戏

1.对于限定猜数字次数

(1)while循环 

number = 56
count = 0
while count < 3:
  guess_number = int(input("guess number:"))
  if guess_number == number:
    print("Yes,you got it.")
    break                              #break 跳出当前循环
  elif guess_number > number:
    print("think smaller...")
  else:
    print("think bigger!")
  count +=1
else:
  print("you have tried too many times..fuck off")

 

(2)for 循环 

number = 56
for i in range(3):
  guess_number = int(input("guess number:"))
  if guess_number == number:
    print("Yes,you got it.")
    break
  elif guess_number > number:
    print("think smaller...")
  else:
    print("think bigger!")
else:
  print("you have tried too many times..fuck off")

2. 任性玩 ,不限制次数

number = 56
count = 0
while count < 3:
  guess_number = int(input("guess number:"))
  if guess_number == number:
    print("Yes,you got it.")
    break
  elif guess_number > number:
    print("think smaller...")
  else:
    print("think bigger!")
  count +=1

  if count == 3:
   countine_confirm = input("do you want to keep guessing")     #如果猜错3次,提示要不要退出游戏 ,输入n表示退出
   if countine_confirm !='n':
    count = 0

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:django-simple-captcha 验证码干扰线随机点位

下一篇:Django基于类的增删改查,简单逻辑都不用写