如何限制用户可以输入的时间

时间:2019-03-08 11:14:34

标签: python-3.x

嗨,我是一个初学者,正在做一个游戏,程序在程序中生成一个随机数,用户需要猜测该数字。如果失败5次,则失败。我需要计算用户可以输入多少次,如果到达5并打印,则迷路并重新启动。

类似这样的东西

cnt = 0 
if cnt >= 5:
   print("You Lost")
   pass

1 个答案:

答案 0 :(得分:0)

您需要:

import random

op_number = random.randint(1,20)
# print(op_number)

number = int(input("Guess a number between 1 to 20 : ")) 
count=0
while True:
    if count == 5:
        print("You Lose.")
        break
    count+=1
    if op_number == number:
        print("You won")
        break
    else:
        print("Incorrect guess. {} attempts left.".format(5-count))

    number = int(input("Guess a number between 1 to 20 : "))