2

我知道每个人都讨厌问任何与 goto 相关的问题的人,但这是我的问题:我是编码的绝对初学者,所以我正在制作我 11 岁的兄弟提出的一些小练习程序。这是一个程序,您可以在其中选择一个项目,它会从一个帐户中取款。

#!/usr/bin/env python
Acc = 30
print("$1 products: Pepsi, Water, Coke, Sprite")
print("$2 products: Chips, Cookies, Skittles, Twix")
print("$3 products: Amp, Monster, Red Bull, Rockstar")
L1 = ('Pepsi', 'Water', 'Coke', 'Sprite')
L2 = ('Chips', 'Cookies', 'Skittles', 'Twix')
L3 = ('Amp', 'Monster', 'Red Bull', 'Rockstar')
sel = raw_input("Please enter a product: ")
if sel in L1:
    print("$1 has been removed from your account.")
    Acc = Acc-1
    print("You now have $") (Acc), ("left in your account")
if sel in L2:
    print("$2 has been removed from your account.")
    Acc = Acc-2
    print("You now have $") (Acc), ("left in your account")
if sel in L3:
    print("$3 has been removed from your account.")
    Acc = Acc-3
    print("You now have $"), (Acc), ("left in your account")

我希望能够从 If 语句的末尾跳转到用户输入部分之前的点。有没有办法在 Python 中做到这一点?提前致谢。(:

4

4 回答 4

3

您应该做的是将您的语句嵌入到while循环中,并在给出特殊值(或不接受)时退出。另外,如果你拿的物品只能在1个列表中,你应该elif声明,所以当找到一个真值时,它就会退出。

#!/usr/bin/env python
Acc = 30
print("$1 products: Pepsi, Water, Coke, Sprite")
print("$2 products: Chips, Cookies, Skittles, Twix")
print("$3 products: Amp, Monster, Red Bull, Rockstar")
L1 = ('Pepsi', 'Water', 'Coke', 'Sprite')
L2 = ('Chips', 'Cookies', 'Skittles', 'Twix')
L3 = ('Amp', 'Monster', 'Red Bull', 'Rockstar')
while True:
    sel = raw_input("Please enter a product: ")
    if sel in L1:
        print("$1 has been removed from your account.")
        Acc = Acc-1
        print("You now have $") (Acc), ("left in your account")
    elif sel in L2:
        print("$2 has been removed from your account.")
        Acc = Acc-2
        print("You now have $") (Acc), ("left in your account")
    elif sel in L3:
        print("$3 has been removed from your account.")
        Acc = Acc-3
        print("You now have $"), (Acc), ("left in your account")
    elif sel == "exit":
        break
于 2012-06-29T04:01:46.790 回答
2

您想要实现的通常是通过在接受用户输入的行之后围绕所有内容的循环来实现的。像这样的东西:

#!/usr/bin/env python
Acc = 30
print("$1 products: Pepsi, Water, Coke, Sprite")
print("$2 products: Chips, Cookies, Skittles, Twix")
print("$3 products: Amp, Monster, Red Bull, Rockstar")
L1 = ('Pepsi', 'Water', 'Coke', 'Sprite')
L2 = ('Chips', 'Cookies', 'Skittles', 'Twix')
L3 = ('Amp', 'Monster', 'Red Bull', 'Rockstar')
while True:
    sel = raw_input("Please enter a product: ")
    if sel == 'exit':
        break
    if sel in L1:
        print("$1 has been removed from your account.")
        Acc = Acc-1
        print("You now have $") (Acc), ("left in your account")
    if sel in L2:
        print("$2 has been removed from your account.")
        Acc = Acc-2
        print("You now have $") (Acc), ("left in your account")
    if sel in L3:
        print("$3 has been removed from your account.")
        Acc = Acc-3
        print("You now have $"), (Acc), ("left in your account")
于 2012-06-29T03:59:41.273 回答
1

您可以使用此技巧保存两次键入所有项目

L1 = ('Pepsi', 'Water', 'Coke', 'Sprite')
L2 = ('Chips', 'Cookies', 'Skittles', 'Twix')
L3 = ('Amp', 'Monster', 'Red Bull', 'Rockstar')
print("$1 products:", ", ".join(L1))
print("$2 products:", ", ".join(L2))
print("$3 products:", ", ".join(L3))
于 2012-06-29T04:17:05.557 回答
0

您通常会为此使用循环:

foo = 42
# … other setup …
while True:
    sel = raw_input(…)
    if sel == "foo":
        do_stuff()
    # … etc …
于 2012-06-29T03:59:25.443 回答