2012-06-29 60 views
2

我知道每個人都討厭與goto有關的任何問題的人,但這是我的問題: 我是一個絕對的初學者,所以我正在做一些小小的練習程序,我的11歲哥哥提出了這個想法。這裏有一個程序,你可以選擇一個項目,它會從一個賬戶中獲得資金。跳轉到Python中的代碼段?

#!/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中做到這一點?在此先感謝(:

+1

喜鍵入的所有項目兩次,請在下一次把你的代碼,直接進入正題,並使用「{}」按鈕格式化吧:) 。 – Zenon

回答

0

你會通常使用一個循環:

foo = 42 
# … other setup … 
while True: 
    sel = raw_input(…) 
    if sel == "foo": 
     do_stuff() 
    # … etc … 
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") 
+0

好,新的問題。我希望布爾值在acc = 0時變爲false。這樣做有多難?對不起,我還在學習。 – Reece

+0

@Reece,'而Acc> = 0:' –

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 
+0

雖然這本身沒有什麼不對,但我建議遠離'break'語句。相反,分配'select = True'並在'select'和'if sel =='exit'時執行':select = False' – inspectorG4dget

+5

另一方面,我完全支持使用'break'來突破'while'循環。我一直都是這樣編寫代碼的,而'True':'break'結合起來是Python中常見的習慣用法。 – steveha

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))