if命令有两个不同的变量

时间:2013-07-26 07:03:31

标签: python

我有一个用户输入,然后是“if语句”,但在这个if语句中我想检查两个不同的变量?无论我尝试什么,它都无法正常工作。

if hm == 3 and karma == 8:
    print ("hello whatever")

这就是我已经尝试过的但是错了,所以我用什么来检查两个不同的变量,谢谢?

编辑:所以我只是在尝试制作迷你游戏...这是我目前的代码

当它到达问题区域时它会跳到结束消息

import time
x = 1
karma = 0
money = 50
ticket1 = 1





name = input("what is your name")
print ("hello", name)

time.sleep(1.5)

check = input("would you like to start your adventure")

if check == "yes" or "Yes" or "yeah":
    print ("good then lets get going")

else:
    print ("bye loser")
    SystemExit

time.sleep(1.5)


print ("you are walking down a road on the way to the shops, when all of a sudden a homeless man asks you for some change")
time.sleep(1)
hm = input("do you 1. give the man all your money, 2. give him some small change, 3. tell him to eff off")

if hm == "1":
    print("you gave the man all your money, karma + 10, you have no money now")
    money = 0
    karma = 10
    x = 1

if hm == "2":
    print ("you give him $10, karma + 2, you have $40 left")
    money = 40
    karma = 2
    x = 1

if hm == "3":
    print (" you tell the man to fuck off, he looks hungry and disappointed, turns out he died later that day. karma - 5, you have $50 left")
    money = 50
    karma = -5
    x = 2

print(".")
print(".")
print(".")
print(".")



time.sleep(1.5)

print ("a bit further down the road you pass a street salesman selling golden tickets,")
time.sleep(1.5)
print("he say's .. they will be worth it later on in the game , honestly..")
time.sleep(1.5)
check1 = input("do you 1. buy a ticket ($5) 2. don't buy a ticket 3. threaten to punch him unless he gives you a ticket")
time.sleep(1)

if hm == 3 and karma == -5:
    money + 5
    print("you beat the shit out of him and take a golden ticket as well as $5, karma -5, you have", money, "dollars left")
    karma = -10
    ticket1 + 1

if hm== 3:
    print ("he cowers under your influence and gives you a ticket you have ", money, "dollars left, karma - 5")
    karma -5
    ticket1 + 1

if hm == 2:
    print ("you walk on withought buying a ticket, no change in karma, you have ", money, " dollars left")

if hm == 1:
    money -10
    ticket1 +1
    print("you buy a golden ticket, you have ", money, " left")

print("well thats all folks")

4 个答案:

答案 0 :(得分:2)

看起来正确,除了缺少的冒号:

if hm == 3 and karma == 8:
    print ("hello whatever")

答案 1 :(得分:2)

您应该检查hmkarma变量是否实际包含整数。可能最简单的方法是在if之前添加以下行:

print type(hm)
print type(karma)

if hm == 3 and karma == 8:
    print ("hello whatever")

如果输出显示<type 'str'>之类的内容,那么您需要将字符串转换为整数或仅与字符串进行比较:hm == '3'

答案 2 :(得分:1)

如果有问题:

if check == "yes" or "Yes" or "yeah":
    print ("good then lets get going")

else:
    print ("bye loser")
    SystemExit

收取费用:

if check.lower() == "yes" or check.lower() == "yeah":
    print ("good then lets get going")
else:
    print ("bye loser")
    sys.exit()

sys需要

import sys

答案 3 :(得分:-2)

错误,你的意思是“钱”和“ticket1”没有更新吗?

if hm == 3 and karma == -5:
   money + 5
   print("you beat the shit out of him and take a golden ticket as well as $5, karma -5, you have", money, "dollars left")
   karma = -10
   ticket1 + 1

但我猜你想要实际更新money和ticket1的值,所以不要像“money + 5”那样想要像这样添加5:“money + = 5”。在前一种情况下没有任何分配(只是打印,如果你在控制台中执行),但在后一种情况下。对于ticket1而言,也许是为了业力?代码将变为:

if hm == 3 and karma == -5:
   money += 5
   print("you beat the shit out of him and take a golden ticket as well as $5, karma -5, you have", money, "dollars left")
   karma = -10
   ticket1 += 1
相关问题