2

goto に関連することについて質問する人が嫌いなのは誰もが知っていますが、ここに私の問題があります。これはアイテムを選択できるプログラムで、アカウントからお金が引き出されます。

#!/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

このトリックを使用すると、すべての項目を 2 回入力する手間を省くことができます

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 に答える