我得到最后一个if
语句中的最后一个guess
的警告。我能忽略它,确保我的程序不会出错吗?
import random
print('Try to guess hidden number from 1 to 50, U have 6 tries by the way')
hidden_num = random.randint(1, 50)
tries = 6
while tries>0:
guess = int(input(f'Guess the hidden number, tries left: {tries}n'))
tries-=1
if guess == hidden_num:
print('Great Job, You got it!')
break
elif guess < hidden_num:
print('U were so close, take a bit higher')
else:
print('WowWo! Pick lower,Dude!')
if guess != hidden_num and tries == 0:
print('Don't be upset buddy, you have whole life ahead!')
解决了警告问题,只是通过添加最后一个if条件,但仍然不明白这两种情况之间的代码执行差异是什么:
if guess != hidden_num and tries == 0:
print('Don't be upset buddy, you have whole life ahead!')
:
if guess != hidden_num and tries == 0:
print('Don't be upset buddy, you have whole life ahead!')