Python 3 - 获取变量的多个输入

0 投票
1 回答
764 浏览
提问于 2025-04-18 02:11

我想让用户可以在收银机里输入多个数值。当他们输入完毕后,只需要输入-1,这样程序就不会再要求输入更多的数值了。完成这个步骤后,程序会把输入的数字加起来,然后以netPrice的形式显示出来。不过,grossPrice会在加总的基础上再加上10%的消费税。对于raw_input和持续变量,我没有什么经验,搜索了一些信息后尝试把它们应用到代码中,但没有成功。

#x = continued input ("Enter in net price of items for the cash register: ")
#if x -1:
#   stop;
#netPrice = x
#grossPrice = netPrice + (0.1 * netPrice)

#print ("The net price of all items is: $",netPrice)
#print ("The gross price of all items (this includes GST) is: $",grossPrice)

x = raw_input("Enter in net price of items for the cash register: ")
if x >=-1:
    stop;
netPrice = x
grossPrice = netPrice + (0.1 * netPrice)

print ("The net price of all items is: $",netPrice)
print ("The gross price of all items (this includes GST) is: $",grossPrice)

1 个回答

0

你可以这样做:

values = []

while True:
    x = float(input('Enter in net price of items for the cash register: '))
    if x == -1:
        break
    values.append(x)

print ("The net price of all items is: " + str(sum(values)))

撰写回答