|
|

楼主 |
发表于 2017-12-29 15:43:21
|
显示全部楼层
我又加强了一下代码 实现了每次提示剩余机会次数,以及chance和chances单复数的问题,感觉好棒哈哈哈

- import random
- secret = random.randint(1,20)
- i = 1
- temp = input("Please give any number in between 1 to 20:")
- guess = int(temp)
- while (guess != secret) and i <6:
- #判断大小
- if guess > secret:
- print ("Number is too big")
- else:
- print("Number is too small")
- #当剩下一次机会的时候chance显示为单数,当剩下0次机会的时候不重复显示提示
- if (i != 4) and (i != 5):
- print("You have", 5 - i, "chances left")
- if i == 4:
- print("You have 1 chance left")
- #在机会还没用进的时候继续输入
- if i < 5:
- temp = input("Please try again:")
- guess = int(temp)
- #机会全部用尽提示
- else:
- print ("Chances are used up")
- #计数器
- i = i + 1
- if guess == secret:
- print("Yes you are right!")
- print ("Game over!")
复制代码 |
|