简体   繁体   English

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

[英]Why does the code always say 'The Number is Smaller"?

I'm a beginner in python and today I tested out my knowledge by making a Number guessing game. 我是python的初学者,今天我通过制作一个数字猜谜游戏测试了我的知识。 Whenever I try to run it, it works fine, however it always says that the number is lower than my guess. 每当我尝试运行它时,它都可以正常工作,但是它总是说该数字低于我的猜测。

I can't figure out the problem, so I haven't really tried any solutions. 我无法解决问题,所以我还没有真正尝试过任何解决方案。

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()

You mixed the signs in these if conditions: 您在以下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 should be if guess < number and vice-versa. if guess > number应该是if guess < number ,反之亦然。

If guess is greater than number it means it means number is smaller, and vice-versa. 如果guess大于number ,则表示number较小,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM