2016-11-10 26 views
-1

我學習的第一次(初學者)蟒蛇,我不得不作出這樣的程序:Python:程序錯誤? (初級)

在產品已經根據他們的代碼折扣商店:

•用代碼的產品45612有10%的折扣

•與代碼的產物45613有12%的折扣

•與代碼45614的產品具有18%的折扣

•產品與代碼45615有20%的折扣

寫一個算法,讀取產品的代碼(int)和值(浮動),並計算折扣後的價格。

如果您輸入的代碼不存在,它應打印「的密碼是不正確的。

但我的程序不能正常工作,我究竟做錯了什麼?

這是我的代碼:

kodikos = [45612, 45613, 45614, 45615] 

password = input("Give code: ") 

timi = float(input("Give price: ")) 



if password == 45612: 

    timi = timi - 10 
    print("The price after the discount is %f" %(timi)) 

if password == 45613: 

    timi = timi - 12 
    print("The price after the discount is %f" %(timi)) 

if password == 45614: 

    timi = timi - 18 
    print("The price after the discount is %f" %(timi)) 

if password == 45615: 

    timi = timi - 20 
    print("The price after the discount is %f" %(timi)) 


for n in kodikos: 

    if(n != password): 
     print("The password doesn't exist!") 
     break 
+1

'input()'給你一個字符串對象,你在比較你的if語句中的int對象。你不能比較蘋果橙子 – MooingRawr

+1

我不知道。你做錯了什麼? –

+0

你還應該檢查給定的'code'輸入是否首先對if語句有效,如果代碼有效然後應用折扣,否則告訴用戶無效的代碼。也可以使用'elif'來鏈接相似的塊 – MooingRawr

回答

0

您減去一個固定的量,但必須減去的相對大寫金額(即10%的值的百分之)嘗試驗證碼。

kodikos = [45612, 45613, 45614, 45615] 

password = input("Give code: ") 

timi = float(input("Give price: ")) 



if password == 45612: 

    timi = timi - 0.1 * timi 
    print("The price after the discount is %f" %(timi)) 

if password == 45613: 

    timi = timi - 0.12 * timi 
    print("The price after the discount is %f" %(timi)) 

if password == 45614: 

    timi = timi - 0.18 * timi 
    print("The price after the discount is %f" %(timi)) 

if password == 45615: 

    timi = timi - 0.2 * timi 
    print("The price after the discount is %f" %(timi)) 


for n in kodikos: 

    if(n != password): 
     print("The password doesn't exist!") 
     break 
+1

不是唯一的問題....你的答案仍然很多問題。 – MooingRawr

+0

是的,我知道我修復了他們,其他答案也幫助我! –