Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:使用while True循环在列表上追加整数_Python_Python 3.x - Fatal编程技术网

Python 3:使用while True循环在列表上追加整数

Python 3:使用while True循环在列表上追加整数,python,python-3.x,Python,Python 3.x,我正在尝试做以下练习: 任务4 创建一个空的采购金额列表 使用用户输入的项目价格填充列表继续添加到列表中,直到输入“完成” 可以在正确时使用:带中断 打印购买金额 以下是我目前掌握的代码: #[ ] complete the Register Input task above purchase_amounts=[] purchase_amounts.append(input("Enter the prices: ")) while True: if input(&qu

我正在尝试做以下练习:

任务4

创建一个空的采购金额列表 使用用户输入的项目价格填充列表继续添加到列表中,直到输入“完成”

可以在正确时使用:带中断

打印购买金额

以下是我目前掌握的代码:

#[ ] complete the Register Input task above
purchase_amounts=[]
purchase_amounts.append(input("Enter the prices: "))
while True:
    if input("Enter the prices: ") != "done":
        purchase_amounts.append(input("Enter the prices: "))
    else:
        break
print(purchase_amounts)
但它给了我一个非常奇怪的输出,如下所示:

输入价格:2222222

输入价格:1

输入价格:2

输入价格:3

输入价格:完成

输入价格:完成

['222222','2','done']

有人知道为什么它会覆盖第二、第四和第五个输入,而不会将值添加到列表中吗?非常感谢

您有2个input(),但实际上只使用一个。查看评论:

while True:
    if input("Enter the prices: ") != "done":                   #Here you only compare the input with "done" but you don't do anything with it
        purchase_amounts.append(input("Enter the prices: "))    #here you ask for a second input
您只能请求输入一次,并将其保存在变量中:

while True:
    value_input = input("Enter the prices: ")
    if value_input != "done":                   
        purchase_amounts.append(value_input)    
    else:
        break
PS:您可能希望将字符串转换为int,否?

您有2个input(),但实际上只使用一个。查看评论:

while True:
    if input("Enter the prices: ") != "done":                   #Here you only compare the input with "done" but you don't do anything with it
        purchase_amounts.append(input("Enter the prices: "))    #here you ask for a second input
您只能请求输入一次,并将其保存在变量中:

while True:
    value_input = input("Enter the prices: ")
    if value_input != "done":                   
        purchase_amounts.append(value_input)    
    else:
        break

PS:您可能想将字符串转换为int,否?

您接受输入的次数太多了

通过调用
input()
&然后添加到列表中,每个循环只能接受一次输入。您可能还希望将输入转换为带有
浮点的数字

purchase_amounts=[]
while True:
    user_input = input("Enter the prices: ")
    if user_input != "done":
        purchase_amounts.append(float(user_input))
    else:
        break
print(purchase_amounts)
输出:

Enter the prices: 12
Enter the prices: 13
Enter the prices: done
[12, 13]


您接受输入的次数太多

通过调用
input()
&然后添加到列表中,每个循环只能接受一次输入。您可能还希望将输入转换为带有
浮点的数字

purchase_amounts=[]
while True:
    user_input = input("Enter the prices: ")
    if user_input != "done":
        purchase_amounts.append(float(user_input))
    else:
        break
print(purchase_amounts)
输出:

Enter the prices: 12
Enter the prices: 13
Enter the prices: done
[12, 13]


尝试将测试用例与IF分开使用

请注意,python的方式为!=is
not(测试用例)

采购金额=[]

#购买金额。追加(输入(“输入价格:”)从IF中分别尝试使用测试用例

请注意,python的方式为!=is
not(测试用例)

采购金额=[]

#购买金额。追加(输入(“输入价格:”))您需要对其进行一些调整
input()
返回用户输入的字符串。在while循环中,如果值不等于“done”,则丢弃该值,并再次提示用户。因此,将该值存储在某个位置,然后与该(存储的)值进行比较。这是因为,您要调用输入函数两次。将输入存储在一个局部变量中,并比较该局部变量。您需要稍微重新构造它
input()
返回用户输入的字符串。在while循环中,如果值不等于“done”,则丢弃该值,并再次提示用户。因此,将该值存储在某个位置,然后与该(存储的)值进行比较。这是因为,您要调用输入函数两次。将输入存储在局部变量中,并比较局部变量。