Python虽然循环困难

时间:2013-06-28 10:40:46

标签: python while-loop

我是一名全新的程序员,我一直在努力学习Python(2.7)。我发现了一些在线练习,其中一个涉及创建一个简单的猜谜游戏。

尽我所能,我无法弄清楚我的代码有什么问题。如果第一次正确猜出数字,则其中的while循环正确执行。此外,如果在第一次尝试时猜到较低的数字,则执行正确的代码块 - 但随后所有后续的“猜测”都会产生“更高”数字的代码块,而不管输入如何。我已经在整个代码中打印出变量来试图看看发生了什么 - 但它没有帮助。任何见解将不胜感激。谢谢!这是我的代码:

from random import randint

answer = randint(1, 100)
print answer
i = 1


def logic(guess, answer, i):
    guess = int(guess)
    answer = int(answer)
    while guess != answer:
        print "Top of Loop"
        print guess
        print answer
        i = i + 1
        if guess < answer:
            print "Too low. Try again:"
            guess = raw_input()
            print guess
            print answer
            print i

        elif guess > answer:
            print "Too high. Try again:"
            guess = raw_input()
            print guess
            print answer
            print i

        else:
            print "else statement"

    print "Congratulations! You got it in %r guesses." % i


print "Time to play a guessing game!"
print "Enter a number between 1 and 100:"
guess = raw_input()
guess = int(guess)

logic(guess, answer, i)

我确信这是显而易见的事情,如果我只是愚蠢的话,我会提前告知。

4 个答案:

答案 0 :(得分:3)

您注意到raw_input()返回一个字符串(正如我在代码底部注意到的那样)。但是你忘了在while循环中将输入更改为整数。

因为它是一个字符串,所以它总是大于一个数字(“hi”&gt; n),因此这就是为什么总是要调用"Too high. Try again:"

因此,只需将guess = raw_input()更改为guess = int(raw_input())

即可

答案 1 :(得分:0)

试试这个:

guess = int(raw_input())

raw_input.__doc__所述,返回类型为string(您需要int)。这意味着您要将intstring进行比较,这会导致您获得的看似错误的结果。有关详细信息,请参阅this answer

答案 2 :(得分:0)

好的,我发现了你的问题。问题出在本守则中:

    if guess < answer:
        print "Too low. Try again:"
        guess = raw_input()
        print guess
        print answer
        print i

    elif guess > answer:
        print "Too high. Try again:"
        guess = raw_input()
        print guess
        print answer
        print i

在上面的代码中,您将输入作为字符串,但您尝试将其与整数进行比较。您需要做的就是将输入转换为整数,如下所示:

guess = raw_input()
guess = int(guess)

这可以解决你的问题:)

答案 3 :(得分:-1)

我更新了程序。你是从while循环中出来的,因为在elif组中输入猜测之后你会忘记将它转换为int,所以它会抛出错误。现在,纠正了,但你也可以优化它。

import sys

from random import randint

answer =  randint(1, 100)
'''randint(1, 100)'''
print (answer)
i = 1


def logic(guess, answer, i):
    guess = int(guess)
    answer = int(answer)
    while guess != answer:
        print ("Top of Loop")
        print (guess)
        print (answer)
        i = i + 1

        if guess < answer:
            print ("Too low. Try again:")
            guess = int(input())
            print (guess)
            print (answer)
            print (i)

        elif guess > answer:
            print ("Too high. Try again:")
            guess = int(input())
            print (guess)
            print (answer)
            print (i)

        else:
            print ("else statement")

    print ("Congratulations! You got it in %r guesses." % i)


print ("Time to play a guessing game!")
print ("Enter a number between 1 and 100:")
guess = input()
guess = int(guess)

logic(guess, answer, i)