0 <2 = ......错?

问题描述 投票:-2回答:2

Python 3.7

试图做一个数字猜谜游戏。这是我的代码:

#This is the guess the number game!
import random
secretNumber = random.randint(0,20)

print('I am thinking of a secret number between 0 and 20, can you guess what it is in six tries or less?')

for guessTimes in range(1,7):
    try:
        guessNumber = int(input())
        print(type(guessNumber))
        print('Guess Number: ' +str(guessNumber))
    except ValueError:
        guessNumber = None
        print('Secret Number: ' +str(secretNumber))
        print('You have to give me a valid integer for your guess.', end = ' ')
    if guessNumber and guessNumber < secretNumber:
        print('You have guessed a number that is too low...')
    elif guessNumber and guessNumber > secretNumber:
        print('You have guessed a number that is too high...')
    elif guessNumber and guessNumber == secretNumber:
        print('Great job, you guessed the secret number in only ' +str(guessTimes)+ ' tries!')
        print('You should go PRO!')
        break
    if guessTimes > 6:
        print('Sorry, that was six tries and lots of fail, better luck next time!')
    elif guessTimes == 1:
        print('That is ' +str(guessTimes)+ ' failed attempt thus far, you have ' +str(6-guessTimes)+ ' chances remaining. Please try again.')
    elif guessTimes > 1:
        print('That is ' +str(guessTimes)+ ' failed attempts thus far, you have ' +str(6-guessTimes)+ ' chances remaining. Please try again.')
    guessTimes += 1
    continue

这一切似乎对于guessNumber的所有值都适用,介于1和123,412,351之间。但由于某种原因,每当我输入0作为guessNumber时,代码都会跳过这一行:

if guessNumber and guessNumber < secretNumber:
        print('You have guessed a number that is too low...')

对不起,这不是一个超级相关的问题,但是我现在已经对这个问题喋喋不休了一段时间,并且无法弄清楚为什么当guessNumber == 0时该行没有评估为True。

python-3.x conditional boolean-logic
2个回答
1
投票

None0都是Python中的“falsy”,因此在测试if guessNumber and guessNumber < secretNumber:guessNumber短路,因为它的00是假的。

你需要缩小你的支票以便只捕捉None,将if guessNumber and ...:形式的测试改为if guessNumber is not None and ...:

另一种解决方案是避免在给出无效值时完全检查猜测。你可以通过从try / except扩展到try / except / else来相当容易地做到这一点:

for guessTimes in range(1,7):
    try:
        guessNumber = int(input())
        print(type(guessNumber))
        print('Guess Number: ' +str(guessNumber))
    except ValueError:
        print('Secret Number: ' +str(secretNumber))
        print('You have to give me a valid integer for your guess.', end = ' ')
    else:
        # Only run when no ValueError occurs
        if guessNumber < secretNumber:
            print('You have guessed a number that is too low...')
        elif guessNumber > secretNumber:
            print('You have guessed a number that is too high...')
        elif guessNumber == secretNumber:
            print('Great job, you guessed the secret number in only ' +str(guessTimes)+ ' tries!')
            print('You should go PRO!')
            break

    if guessTimes == 1:
        print('That is ' +str(guessTimes)+ ' failed attempt thus far, you have ' +str(6-guessTimes)+ ' chances remaining. Please try again.')
    else:
        print('That is ' +str(guessTimes)+ ' failed attempts thus far, you have ' +str(6-guessTimes)+ ' chances remaining. Please try again.')
else:
    # Run when loop exits without break
    print('Sorry, that was six tries and lots of fail, better luck next time!')

这里的优点是你不需要设置或测试guessNumber,当它是垃圾时(不使用None作为标志值等),所以当它不是垃圾时,你不需要检查None的旗帜一点都不

旁注:在新代码中,我删除了guessTimes += 1continue,因为for i in range(1, 7):已经为你做了增量,并且continue作为循环中的最后一个语句是荒谬的。我还将“太多的猜测”输出移动到else循环上的for;你从来没有按照所写的那样编写代码(因为循环以guessTimes结尾为6guessTimes > 6总是为假),并且它会保存检查以使循环在没有break的情况下自动发生。


0
投票

如果在if等条件语句中使用的数字被视为boolean(如果为0则为false,否则为true)

if guessNumber and guessNumber < secretNumber:
     print('You have guessed a number that is too low...')

if语句中有两个条件,第一个条件为false,因为输入0的guessNumber为false,因此跳过该行。

从条件中删除guessNumber,你很好

if guessNumber < secretNumber:
     print('You have guessed a number that is too low...')
© www.soinside.com 2019 - 2024. All rights reserved.