输入我的号码后,猜猜游戏的代码不会打印任何内容

时间:2015-02-05 22:50:40

标签: python python-2.7

我正在尝试用python制作一个猜谜游戏,但我的代码似乎无法工作。我刚刚进入python所以我不是最好的。这是我的代码。

print "Hello"
print "You have found me, haven't you?"
print "Well, since you did all the work to find me..."
print "I will let you have my diamond and gold infused microwave!"
print "But there is a twist"
print "You have to guess my favorite number! You only have one try!"
print "It is a number from 1 to 5"

guess=raw_input("What is my number?")

import random

for x in range(1):
  print random.randint(1,5)

correct=random.randint

def correct_number(correct):
    if correct==guess:
        print "Dang! You got it!"
    elif correct > guess:
        print "Wrong! Too low!"
    elif correct < guess:
        print "Wrong! Too High!"

我需要它说&#34; Dang it!你赢了!&#34;如果你做对了,&#34;错了!太高了!&#34;如果你的猜测太高了,那就错了!太低了!&#34;如果你的猜测太低了。

2 个答案:

答案 0 :(得分:1)

correct=random.randint

这会将correct设置为产生随机数的函数,而不是随机数。

关于代码有很多错误或“怪异”,但这是导致错误的原因。你应该改为调用函数

correct = random.randint(1,5)

说到调用函数,你也从不调用correct_number函数。你可能应该这样做:

guess = raw_input("what is my number? ")
correct = random.randint(1,5)

def correct_number():
    if guess == correct:
        # yay
    if guess < correct:
        # too low
    if guess > correct:
        # too high

correct_number()

答案 1 :(得分:1)

问题在于这一行

correct=random.randint

random.randint是一个功能。

>>> import random
>>> random.randint
<bound method Random.randint of <random.Random object at 0x7ffa7b072220>>

所以你将函数分配给变量correct,而不是函数的结果,这就是你想要的。如果你把它改成以下它应该有用。

correct=random.randint(1, 5)