【问题标题】:Returning wrong values with if in python in DEF() statement在 DEF() 语句中的 python 中使用 if 返回错误值
【发布时间】:2017-11-30 14:05:21
【问题描述】:

我最近开始使用 python 3.0,并且一直在编写 Hazard 赌场游戏,并且我使用这样的 if 语句:

def starthazard():
global chips
Main = input ("Main")
time.sleep(0.1)
if Main == "?" :
print("                                Hazard Rules...                         ")
time.sleep(0.5)
print("You can main 6,7,8 or 9. The main your luckiest number")
time.sleep(0.5)
print("Nicks is winning either 3 / 1 if main is rolled but 1 / 2 if chance is rolled.")
print("Rolling your main is nicks")
print("2 or 3 is thrown out")
time.sleep(0.5)
print("with a main of 5 or 9, you throw out with both an 11 and a 12")
print("with a main of 6 or 8, you throw out with an 11 but nick with a 12")
print("with a main of 7, you nick with an 11 but throw out with a 12")
print("Other numbers are Chance, Roll again but this time main is out and Chance is nicks")
time.sleep(6.9)
Main = input ("Main")
Bet = input("you have " + str (chips) + " chips, what bet would you like to 
place?")
dice1HAZARD = random.randint(1,6)
dice2HAZARD = random.randint(1,6)
RESULTHAZARD = dice2HAZARD + dice1HAZARD
print("first dice is... " + str (dice1HAZARD))
time.sleep(1)
print("second dice is " + str (dice2HAZARD))
time.sleep(1)
print("therefore your number is "+ str (RESULTHAZARD))
time.sleep(1)
if RESULTHAZARD == 2 or 3 :
  print("THROWN OUT! MINUS " + Bet + " Chips" )
  chips = chips - int (Bet)
  PLAYAGAIN = input("play again? Y/N")
  if PLAYAGAIN == "Y":
    starthazard()
  else:
    PICKGAME()
if RESULTHAZARD == Main :
  print("NICKS 3/1!")
  CHIPSWON = Bet * 3
  chips = chips + CHIPSWON
  PLAYAGAIN = input("play again? Y/N")
  if PLAYAGAIN == "Y":
    starthazard()
  else:
    starthazard()
def CHANCECHECK():
  if RESULTHAZARD != Main or 2 or 3 or 11 or 12:
    print("CHANCE" + str (RESULTHAZARD) )
    dice1HAZARD = random.randint(1,6)
    dice2HAZARD = random.randint(1,6)
    RESULTHAZARD = dice2HAZARD + dice1HAZARD
    print("first dice is... " + str (dice1HAZARD))
    time.sleep(1)
    print("second dice is " + str (dice2HAZARD))
    time.sleep(1)
    print("therefore your number is "+ str (RESULTHAZARD))
    time.sleep(1)
    CHANCECHECK()

为什么返回 this : 主要7

你有 800 个筹码,你想下什么赌注? 10

第一个骰子是... 6

第二个骰子是 1

所以你的号码是 7

扔掉!减去 10 个筹码

再玩一次?是/否

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    如果 RESULTHAZARD 为 2,RESULTHAZARD == 2 or 3 的计算结果为 True or 3,其计算结果为 True。否则,RESULTHAZARD == 2 or 3 的计算结果为 False or 3,其计算结果为 3,这是一个真实值。

    您需要明确比较两者是否相等。使用RESULTHAZARD == 2 or RESULTHAZARD == 3RESULTHAZARD in (2, 3)

    对于!= 运算符,可以使用与and 相关的多个检查,或者将not in 与感兴趣的值序列一起使用。

    【讨论】:

    • != 语句怎么样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2017-04-12
    相关资源
    最近更新 更多