2015-02-05 30 views
-2

我想用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!" 

我需要它說「當它!你贏了!」如果你做得對,「錯!太高!」如果你的猜測太高,並且「錯誤!太低!」如果你的猜測太低。

+0

確保正確格式化您的代碼,以便它可以很容易地閱讀:http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks – ari 2015-02-05 22:53:28

+1

什麼不加工? – IanAuld 2015-02-05 22:56:35

+0

*「我的代碼似乎不起作用」* - 這是一個沒有意義的描述。什麼不行?你有錯誤嗎?如果是這樣,你的錯誤是什麼?你問你爲什麼不像你期望的那樣行事? – yuvi 2015-02-05 22:56:50

回答

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

問題是此行

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)