2016-08-14 67 views
-1

所以我有我的代碼在這裏爲我使用PyDev在Eclipse霓虹燈Python 3.4中創建的骰子滾動模擬器遊戲。當你猜一個數字,從1-6的值將隨機生成,如果你得到多少吧,你繼續前進等Python 3.4骰子滾動模擬器代碼錯誤

guess1 = input("Enter your first guess as to which side the dice will roll on (1-6): ") 

import random 
dice_side = ['1','2','3','4','5','6'] 

game = print(random.choice(dice_side)) 

if guess1 == game: 
    print("Congrats that's the correct guess!") 
else: 
    print("That's the wrong guess!") 

我測試了代碼,每次我提出一個數字,控制檯始終打印「這是錯誤的猜測!」。即使我猜的數字與生成的數字相匹配。我無法弄清楚這有什麼問題。我在想,也許我應該使用而不是循環。但是,我想知道我是否可以這樣做,這個特定的代碼有什麼問題。我是Python的新手,所以任何幫助表示讚賞。提前致謝!

+0

想想'print'返回什麼。 – user2357112

回答

1

print()返回None。如果您在該行之後打印game,則會看到它的價值。要解決:

game = random.choice(dice_side) 
0

爲了理解爲什麼你的遊戲不工作,你應該試着先了解什麼是錯的這條線game = print(random.choice(dice_side))。這是你的遊戲的修改版本,這將給你一些線索,試圖運行它,瞭解它,那麼就檢查你的腳本:

import random 

dice_side = ['1', '2', '3', '4', '5', '6'] 

game = random.choice(dice_side) 
print("Dice has been rolled... :D {0}".format(game)) 
guess = -1 
number_attempts = 0 

while True: 
    guess = raw_input("Which side the dice has rolled on (1-6): ") 
    number_attempts += 1 

    if guess == game: 
     print("Congrats that's the correct guess! You've tried {0} times".format(
      number_attempts)) 
     break 

    print("That's the wrong guess!") 

一個建議,一旦你發現了,爲什麼不工作,只是嘗試添加越來越多的新功能,直到你的遊戲變得真正上癮和有趣......但最重要的是,只是玩得開心:)