-6

我有一个用户输入,然后是一个“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

4 回答 4

2

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

print type(hm)
print type(karma)

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

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

于 2013-07-26T07:10:14.373 回答
2

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

if hm == 3 and karma == 8:
    print ("hello whatever")
于 2013-07-26T07:05:26.630 回答
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()

系统需要

import sys
于 2013-07-26T07:33:17.900 回答
-2

错误,您的意思是“money”和“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的值,所以你想要像这样添加5而不是“money + 5”:“money += 5”。在前一种情况下没有分配(只是打印,如果你在控制台中这样做)但在后一种情况下有。票 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
于 2013-07-26T07:20:24.853 回答