我让猜测者有五次机会来猜这个随机数字.每次猜测者做错了,我都想打印出来:
Nice try, guess again (try bigger), you get {x} chance left.
个
x
是倒计时,所以第一个错误的猜测会说你还有4次机会,然后又一次错误,还有3次机会,以此类推.我怎么能做到这一点?或者,如何在x上添加一个循环.
所以这是我不成功的代码:
import random
secret_num = random.randint(1, 20)
print('Guess the number from 1-20 (you get 5 chances)')
for guess_chance in range(1, 6):
guess = int(input("Input guess number: "))
if guess < secret_num:
x = 5
x -= 1
print(f'Nice try, guess again (try bigger), you get {x} chance left.')
elif guess > secret_num:
x = 5
x -= 1
print(f'Nice try, guess again (try smaller), you get {x} chance left.')
else:
break
if guess == secret_num:
print('Congratz, you guess correctly in' + str(guess_chance) + 'times.')
else:
print('Nope, the correct number is ' + str(secret_num) + '.')