"而"逻辑运算符使程序无法工作

时间:2018-01-13 00:38:32

标签: python

我已经学习了大约4天的Python,而且我只处理我的第一个问题。

import random
number=random.randint(1,10)
count=1
guess= int(input("Enter your guess between 1 and 10 : "))

while number != guess:
    count = count + 1

if guess == number:
    print("That is my number !")

while guess < number:
    guess = int(input("Too low :( Guess again ! : "))
    if guess == number:
        print("That is my number !")

while guess > number:
    guess = int(input("Too high :( Guess again ! : "))
    if guess == number:
        print("That is my number !")

我的程序只打印第一个输入行,然后没有打印。

Enter your guess between 1 and 10 : 

为什么?

while number != guess:
    count = count + 1

当我删除这两行时,它完美无缺。

1 个答案:

答案 0 :(得分:2)

在Python中,whitspace很重要,因为循环没有正确缩进,你的程序没有按预期工作。更正后的代码如下所示:

import random
number = random.randint(1,10)
count = 1 
guess = int(input("Enter your guess between 1 and 10 : "))

while number != guess:
    count = count + 1

    if guess == number:
        print("That is my number !")

    elif guess < number:
        guess = int(input("Too low :( Guess again ! : "))

    else:
        guess = int(input("Too high :( Guess again ! : "))