python 我需要一个代码块来退出if语句[closed]

wkftcu5l  于 2022-10-30  发布在  Python
关注(0)|答案(5)|浏览(145)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
22小时前关门了。
Improve this question
下面是一个示例代码:
var1 = 2f
var1 = 2f
if var1 == 2f:
if money < 25:
print("you dont have enough money")
在这之后,我需要一些东西来退出if语句(以及更大的一个)。
可能吗?有没有break之类的东西,但用于if语句?

item1 = "2f"
money = 20
inventory = []
if item1 == "2f":
    if money < 25:
        print("sorry mate, you don't have enough")
        #this is where i got stuck
    print("you bought the large cookie box for 25 dollars. ")
    money -= 25
    print("you now have", money, "dollars")
    inventory.append("LCB")

它只是简单地打印了那个并附加了LCB,使货币为-5,然后打印了另一条消息。(这是一个大得多的代码的一部分,所以它被编辑了一点。)我不能使用函数方法,因为我稍后会需要列表和变量。我也不能使用循环。我也是一个初学者,我不想得到(太)复杂的答案。
快速编辑:我确实找到了如何解决手头的代码,但我仍然希望一些答案,以帮助在其他项目。

o7jaxewo

o7jaxewo1#

我想你需要的是一个else语句。当条件不满足时,它会做一些事情。在这个例子中,当用户没有足够的钱时。例如:

if money < 25:
        # do something
    else:
        # do something if the user has more or equal money
ztmd8pv5

ztmd8pv52#

将代码更改为:

item1 = "2f"
money = 20
inventory = []
if item1 == "2f":
    if money < 25:
        print("sorry mate, you don't have enough")
        #this is where i got stuck
    else: 
        # this only runs if condition in the if statement above failed
        print("you bought the large cookie box for 25 dollars. ")
        money -= 25
        print("you now have", money, "dollars")
        inventory.append("LCB")
ylamdve6

ylamdve63#

没有看到更宽的代码,我不确定你退出它到底是什么意思。你可以把上面的代码组合成一个if/else语句:

item1 = "2f"
money = 20
inventory = []
if item1 == "2f" and money < 25:
    print("sorry mate, you don't have enough")
else:
    print("you bought the large cookie box for 25 dollars. ")
    money -= 25
    print("you now have", money, "dollars")
    inventory.append("LCB")

显然,您可以根据需要将整个语句放入whilefor循环中

avkwfej4

avkwfej44#

在其他关于添加“else”子句的评论和建议的基础上,下面是一段代码,允许使用不同数量的资金进行一些连续的测试。

inventory = []

while True:

    item1 = input("Please enter the item you wish to purchase: ")

    if item1 == "Quit":
        break
    money = int(input("How much money do you have to spend: "))

    if item1 == "2f":
        if money < 25:
            print("Sorry mate, you don't have enough")
        else:
            print("you bought the large cookie box for 25 dollars. ")
            money -= 25
            print("you now have", money, "dollars")
            inventory.append("LCB")

    else:
        print("Sorry - that item is not in stock")

以下是终端的一些示例输出。

@Dev:~/Python_Programs/Cookies$ python3 Cookies.py 
Please enter the item you wish to purchase: 2f
How much money do you have to spend: 20
Sorry mate, you don't have enough
Please enter the item you wish to purchase: 2f
How much money do you have to spend: 45
you bought the large cookie box for 25 dollars. 
you now have 20 dollars
Please enter the item you wish to purchase: Quit

沿着其他建议,你可能给予一试。

mmvthczy

mmvthczy5#

如果你真的有一堆“裸”条件,那么你得到的答案是正确的。如果这只是你的MRE,而你实际上在def中有所有这些,那么return很可能是你想要的。如果你可以在运行购买代码之前完全退出,那么就没有必要创建else......这就是return所做的。

item1     = "2f"
inventory = []
money     = 40

def buy(item):
    global money

    if item == "2f":
        if money < 25:
            print("sorry mate, you don't have enough")
            return #exits the function entirely

        print("you bought the large cookie box for 25 dollars. ")
        money -= 25
        print("you now have", money, "dollars")
        inventory.append("LCB")

buy(item1)

扩展@NoDakker的例子,continue才是你真正想要的。我认为,首先要做的是看看商品是否有效,然后检查钱。你的整个商店几乎可以在任何条件下建立起来。

inventory = []
items     = ('ball', 'sword', 'potion')
prices    = (10, 20, 30)
money     = 50

choice    = lambda: input('Buy something else? (y or n)') == 'n'

while True:
    print(f'items: {items}')
    item = input("Please enter the item you wish to purchase: ")

    try: price = prices[items.index(item)]
    except: 
        print("Sorry, but that item is not in stock.")
        if choice(): break # ends the loop
        continue           # skips to the next iteration

    if money<price: 
       print("Sorry, mate. You don't have enough money.")
       if choice(): break # ends the loop
       continue           # skips to the next iteration

    #if the loop got this far the item exists and they have the money for it
    money -= price
    inventory.append(item)

    print(f"You bought the {item} for {price} dollars. ")
    print(f"You have {money} dollars remaining.")

    if choice(): break #ends the loop

相关问题