我如何限制用户只能输入3次数字

时间:2019-07-29 18:20:51

标签: python

from random import randrange
A = input ("enter number ")
B = randrange (1,10)
B = int (B)
A = int (A)
while A != B :
 B = randrange(1, 10)
 if A == B :
  print ("you guessed the number")
 elif A != B :
  input  ("incorrect number try again ")

这是我的代码,我想限制用户只能输入3次

1 个答案:

答案 0 :(得分:0)

您可以在每次输入数字时进行计数并减少计数。

from random import randrange
A = input ("enter number ")
B = randrange (1,10)
B = int (B)
A = int (A)
counter = 3
while A != B  and counter:
 counter -= 1
 B = randrange(1, 10)
 if A == B :
  print ("you guessed the number")
 elif A != B :
  input  ("incorrect number try again ")