[英]python: if statement, not responding properly to value from dice roll
所以我一直在用python進行基於輸入的地牢游戲,這是非常基礎的。 我想在游戲中加入擲骰子的條件,並決定玩家會死還是在路上生存。 但是,我的if語句不能正確響應程序中編號生成器的滾動,它將打印編號,然后繼續進行是否滿足條件。 我該如何解決這個問題,為什么會這樣?
if direction == 1:
import random
from random import *
num = 6
def d6(num):
rolls = []
for x in range(num):
rolls.append(randint(1,6))
return rolls
rolls = d6(1)
print rolls
if rolls is 3:
print "you fall in a hole!\n"
print ' OH DEAR YOU ARE DEAD!'
raise SystemExit
elif rolls is not 3:
print "you are back at the start which way will you go?\n"
path3 =float(input("forward, or right?"))
is
和is not
保留用於python中的特殊比較。 您需要==
和!=
。 您也可以將其更改為簡單的if / else而不是if / elif。 同樣,您的d6
函數看起來返回的是列表而不是單個數字。 嘗試調整該值以返回單個數字:
from random import randint
def d6():
return randint(1,6)
rolls = d6()
if rolls == 3:
print "you fall in a hole!\n"
print ' OH DEAR YOU ARE DEAD!'
raise SystemExit
else:
print "you are back at the start which way will you go?\n"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.