0 <2 = ...错误?

时间:2019-02-22 04:23:05

标签: python-3.x conditional boolean-logic

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

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

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

很抱歉,这不是一个超级相关的问题,但是我已经将我的头撞了好一阵子了,无法弄清楚为什么当guessNumber ==时,该行的计算结果不为True 0。

2 个答案:

答案 0 :(得分:1)

flutterEngine?.run(withEntrypoint: nil)None在Python中都是“虚假的”,因此测试0if guessNumber and guessNumber < secretNumber:会短路,因为它是guessNumber0是虚假的。

您需要缩小支票的范围,使其仅捕获0,并将测试形式从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的标志。

旁注:在新代码中,我删除了NoneguessTimes += 1,因为continue已经为您完成了递增操作,而for i in range(1, 7):作为循环中的最后一条语句是荒谬的。我还将“太多的猜测”输出移到了continue循环的else上;您将永远不会按原样写该代码(因为循环以forguessTimes结尾,6始终为false),并且它保存检查以确保在循环结束时没有guessTimes > 6

答案 1 :(得分: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...')