【发布时间】:2020-11-11 03:09:03
【问题描述】:
在 Python3 中:如何在需要整数的 while 循环中检查字符串...
这是我目前的代码:我想用“quit”或“q”替换标记值“0”。我该怎么做?
import random
highest = 10
lowest = 1
counter = 0
random_number = random.randint(lowest, highest)
# The randint function is in the random function within the random MODULE imported above.
guess = int(input(f"Guess a number between {lowest} and {highest}. Enter '0' at any time to quit: "))
while guess is not random_number:
if guess == 0:
print("Quitting game.")
break
elif guess < random_number:
counter += 1
guess = int(input("Guess higher: "))
elif guess > random_number:
counter += 1
guess = int(input("Guess lower: "))
if guess == random_number:
print(f"Correct! Well done!! You got it right after {counter} guess(es)!")
我想把sentinel值“0”改成“q/quit”,但不知道怎么...
【问题讨论】:
标签: python string while-loop integer sentinel