2014-02-28 19 views
-1

我需要爲每個座位輸入一個值,並在用戶輸入'q'退出時讓腳本輸出所有座位的總數。這裏是到目前爲止的代碼:我應該如何添加/重做這個python腳本來輸入一個值並打印出所有的?

cost = [] 
print 'Welcome to the receipt program!' 
while True: 
    cost = raw_input("Enter the value for the seat ['q' to quit]: ") 
    if cost == 'q': 
     break 
print '*****' 
print 'Total: $', cost 
+0

這是功課? – tawmas

+0

不,不是......我正在努力學習Python,自己寫一本書。 – user3365803

回答

0
totalCost = 0 
print 'Welcome to the receipt program!' 
while True: 
    cost = raw_input("Enter the value for the seat ['q' to quit]: ") 
    if cost == 'q': 
     break 
    else: 
     totalCost += int(cost) 

print '*****' 
print 'Total: $', totalCost 
+0

謝謝你....現在這裏是我的另一個困境....我改變了int爲float,因爲我想使用小數位...現在我需要驗證輸入是什麼...所以如果用戶輸入字符串(如單詞'five'而不是數字)除q或數字外,它告訴他們「我很抱歉,但'五'無效,請再試一次.....然後它會提示用戶再次輸入我是Python的新手,一直在爲這個簡單的問題絞盡腦汁。 – user3365803

相關問題