为什么代码总是显示“数字较小”?

时间:2019-08-23 22:35:03

标签: python python-3.x

我是python的初学者,今天我通过制作一个数字猜谜游戏测试了我的知识。每当我尝试运行它时,它都可以正常工作,但是它总是说该数字低于我的猜测。

我无法找出问题所在,因此我还没有真正尝试过任何解决方案。

import random

while True:
    print('Welcome! Type "Play" to play. Type "Quit" to Quit the program.')
    choice = input()
    if 'Play' in choice:
        print('Very Well. I am thinking of a number Between 1 and Hundred. Try to Guess it!')
        break
    elif 'Quit' in choice:
        exit()
    else:
        print("Sorry, That's not a valid Function.")
        continue
number = random.randint(1, 100)
GuessesTaken = 0
while GuessesTaken < 13:
    guess = int(input('Enter a Number: '))
    GuessesTaken += 1

    if guess > number:
        print('The Number is bigger than that!')
        continue
    if guess < number:
        print('The number is Smaller than that!')
        continue
    if guess == number:
        print(f'You got it! he Number is {number}! Thanks for Playing!')
        break
if GuessesTaken > 13:
    print(f"Sorry, You're out of Guesses. The number was {number}" )
    exit()

1 个答案:

答案 0 :(得分:2)

您在以下if条件下混合了符号:

if guess > number:
    print('The Number is bigger than that!')
    continue
if guess < number:
    print('The number is Smaller than that!')
    continue

if guess > number应该是if guess < number,反之亦然。

如果guess大于number,则表示number较小,反之亦然。