1. while语句
2. range语句
例:
#while example
number = 59guess_flag = False
while guess_flag == False:
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
guess_flag = True
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that, keep guessing')
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that, keep guessing')
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')
#For example
number = 59
num_chances = 3
print("you have only 3 chances to guess")
for i in range(1, num_chances + 1):
print("chance " + str(i))
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
break #跳出循环
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
# You can do whatever you want in a block ...
else:
print('No, the number is lower than that, keep guessing, you have ' + str(num_chances - i) + ' chances left')
# you must have guessed > number to reach here
print('Done')