Python:程序错误? (初级)

时间:2016-11-10 19:11:54

标签: python python-3.x

我第一次学习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 个答案:

答案 0 :(得分: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