# 有没有一种方法可以根据循环的条件更改字符串的输出？

1. 我希望输出显示剩余的准确机会数量（6个机会，5个机会，4个机会... 0）。我想我可能已经解决了这个问题，但是我相信你们所有人都有更好的方法。
2. 当玩家的机会降到1时，我希望文本从“机会左移”更改为“机会左移”。
``````# This is a guess the number game.
import random

secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')

# Ask the player to guess 6 times.
for guessTaken in range(7, 1, -1):
print('Take a guess. You only have ' + str(guessTaken - 1) + ' chances left... ')
guess = int(input())

if guess < secretNumber:
print('Your guess is too low. ')
elif guess > secretNumber:
print('Your guess is too high. ')
else:
break  # This condition is the correct guess

if guess == secretNumber:
print('Good job! you guessed my number i ' + str(guessTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
``````

• 沉淀！ 回复

如果您还不了解函数，现在可能是个好时机。一个简单的功能是这样的：

``````def chances_remaining_message(num_chances):
if num_chances == 1:
return 'Take a guess. You only have 1 chance left...'
else:
return 'Take a guess. You only have ' + str(num_chances) + ' chances left...'
``````

然后您将在此处致电：

``````# Ask the player to guess 6 times.
for guessTaken in range(7, 1, -1):
print(chances_remaining_message(guessTaken - 1))
guess = int(input())
``````