Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 从用户输入{具有无限用户输入}创建列表_Python_String_List - Fatal编程技术网

Python 从用户输入{具有无限用户输入}创建列表

Python 从用户输入{具有无限用户输入}创建列表,python,string,list,Python,String,List,我试图创建一个函数定义,它要求用户输入一个int或float值以添加到一个空列表中,但继续要求用户输入,直到用户键入-1左右( 因此,最终得到一个包含 销售额是原来的五倍 想想这个: while True: # will loop until a break salesValue = float(input('Please enter a value greater than or equal to zero (-1 to end): ')) if salesValue <

我试图创建一个函数定义,它要求用户输入一个int或float值以添加到一个空列表中,但继续要求用户输入,直到用户键入-1左右( 因此,最终得到一个包含 销售额是原来的五倍

想想这个:

while True: # will loop until a break
    salesValue = float(input('Please enter a value greater than or equal to zero (-1 to end): '))
    if salesValue < 0:
        break
    else:
        salesList.append(salesValue)
为True时:#将循环直到中断
salesValue=float(输入('请输入一个大于或等于零的值(-1到结尾):'))
如果salesValue<0:
打破
其他:
salesList.append(salesValue)

您当前的代码有一个错误。基本上,当流最终到达
for
语句时,
salesValue
的值不会更改,因此程序会附加相同的值5次。请尝试以下操作:

def main():
    salesList = []
    salesValue = float(input())
    while salesValue < 0:
        salesvalue = float(input())
        if salesValue == -1:
            break
    for values in range (5):
        salesList.append(salesValue)
        salesValue = float(input("yourmessage"))
    print salesList

main()
def main():
salesList=[]
salesValue=float(输入())
当salesValue<0时:
salesvalue=float(输入())
如果salesValue==-1:
打破
对于范围(5)中的值:
salesList.append(salesValue)
salesValue=float(输入(“您的消息”))
打印销售清单
main()
您想要什么

while salesValue > 0:
而不是

while salesValue < 0:

还有。

是的,这就是你要求它做的。为什么要用for循环?while true部分在我使用的程序中似乎不起作用。我只熟悉while语句中的布尔表达式。@justachillGamer oops,对不起,有一个拼写错误,应该是true,而不是true。已编辑。
def main():
    salesList = []
    salesValue = float(input())
    while salesValue < 0:
        salesvalue = float(input())
        if salesValue == -1:
            break
    for values in range (5):
        salesList.append(salesValue)
        salesValue = float(input("yourmessage"))
    print salesList

main()
while salesValue > 0:
while salesValue < 0:
if salesValue == -1:
        break