Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 如何允许只输入字符串值";停止“&引用&引用;,然后检查并转换为float_Python_Python 3.x - Fatal编程技术网

Python 如何允许只输入字符串值";停止“&引用&引用;,然后检查并转换为float

Python 如何允许只输入字符串值";停止“&引用&引用;,然后检查并转换为float,python,python-3.x,Python,Python 3.x,嗨,我的循环结束时遇到了问题。我需要将输入作为字符串接受,以获取“stop”或“”,但我不需要任何其他字符串输入。输入被转换为float,然后添加到列表中,但是如果用户键入“bob”,我会得到转换错误,并且我不能设置输入(float),因为这样我就不能接受“stop” 完整的当前代码如下 我目前的想法如下: 检查“停止” 检查输入是否为浮点数 如果不是1或2,则请求有效输入 有什么想法吗?如果是简单的,就给我指个方向,我会试着把它翻出来。否则 谢谢 # Write a progam that a

嗨,我的循环结束时遇到了问题。我需要将输入作为字符串接受,以获取“stop”或“”,但我不需要任何其他字符串输入。输入被转换为float,然后添加到列表中,但是如果用户键入“bob”,我会得到转换错误,并且我不能设置输入(float),因为这样我就不能接受“stop”

完整的当前代码如下

我目前的想法如下:

  • 检查“停止”
  • 检查输入是否为浮点数
  • 如果不是1或2,则请求有效输入
  • 有什么想法吗?如果是简单的,就给我指个方向,我会试着把它翻出来。否则

    谢谢

    # Write a progam that accepts an unlimited number of input as integers or floating point.
    # The input ends either with a blank line or the word "stop".
    
    
    mylist = []
    
    g = 0
    total = 0
    avg = 0
    
    
    def calc():
        total = sum(mylist);
        avg = total / len(mylist);
        print("\n");
        print ("Original list input: " + str(mylist))
        print ("Ascending list: " + str(sorted(mylist)))
        print ("Number of items in list: " + str(len(mylist)))
        print ("Total: " + str(total))
        print ("Average: " + str(avg))
    
    
    while g != "stop":
        g = input()
        g = g.strip()  # Strip extra spaces from input.
        g = g.lower()  # Change input to lowercase to handle string exceptions.
        if g == ("stop") or g == (""):
            print ("You typed stop or pressed enter") # note for testing
            calc() # call calculations function here
            break
    
    # isolate any other inputs below here ????? ---------------------------------------------
            while g != float(input()):
                print ("not a valid input")
        mylist.append(float(g))
    

    我认为蟒蛇式的方式应该是:

    def calc(mylist): # note the argument
        total = sum(mylist)
        avg = total / len(mylist) # no need for semicolons
        print('\n', "Original list input:", mylist)
        print("Ascending list:", sorted(mylist))
        print ("Number of items in list:", len(mylist))
        print ("Total:", total)
        print ("Average:", avg)
    
    mylist = []
    while True:
       inp = input()
       try:
           mylist.append(float(inp))
       except ValueError:
           if inp in {'', 'stop'}:
                calc(mylist)
                print('Quitting.')
                break
           else:
                print('Invalid input.')
    

    我认为蟒蛇式的方式应该是:

    def calc(mylist): # note the argument
        total = sum(mylist)
        avg = total / len(mylist) # no need for semicolons
        print('\n', "Original list input:", mylist)
        print("Ascending list:", sorted(mylist))
        print ("Number of items in list:", len(mylist))
        print ("Total:", total)
        print ("Average:", avg)
    
    mylist = []
    while True:
       inp = input()
       try:
           mylist.append(float(inp))
       except ValueError:
           if inp in {'', 'stop'}:
                calc(mylist)
                print('Quitting.')
                break
           else:
                print('Invalid input.')
    

    谢谢,效果很好。按照我的理解,将其分解。在获得输入后,它将尝试将值作为浮点值附加。如果它无法附加,则将其捕获为ValueError。Exception,If允许通过特殊条件。谢谢,效果很好。按照我的理解将其分解。在获得输入后,它将将尝试将该值附加为浮点值。如果无法附加,则将其捕获为ValueError。Exception、If允许通过特殊条件。