【问题标题】:Python 3- Why is my try and except function in this homework problem not compiling? [closed]Python 3-为什么我在这个作业问题中的 try 和 except 函数没有编译? [关闭]
【发布时间】:2022-01-23 21:25:37
【问题描述】:

我的循环作业需要帮助!

我是 Python 3.7 的新手,遇到一个作业问题,需要我使用多个语句来查找用户输入的最小值和最大值。我知道我必须创建一个循环语句。但是,我卡在提示的第二部分。

我还必须创建一个 try + except 函数,这样当用户输入字符串时,就没有回溯了。

当我尝试编译时,我收到:

SyntaxError: 第 16 行的“返回”外部函数

能否有更高级的 Python 人士,看看我的语法 + 语句,让我知道其中一个地方是否有错误 + 下次要注意什么?

以下是显示原始硬件提示和我尝试解决它的两个屏幕截图。

 
largest = None
smallest = None
fval = float(val)

while True:
    val = input("Enter a number: ")
    fval = float(val)
    if val == "done":
        break
    print(fval)

    if smallest is None:
        smallest = fval
    elif fval > smallest :
        fval = smallest
    return fval

    if largest is None:
        largest = fval
    elif fval > largest :
        largest = fval
    return fval

try :
    int(astr)

except :
    print("invalid output")

print("Minimum", smallest)
print("Maximum", largest)

非常感谢!

Original Homework Prompt

My Code Written Out

【问题讨论】:

  • 请解释一下“不编译”是什么意思。你应该得到一个错误。请分享那个错误。快速浏览一下,错误的发生是因为您没有定义 astr,您尝试在第 24 行将其转换为 int
  • 您好 Pranav,感谢您的帮助!我不断收到“语法错误:第 18 行的错误输入”,这是我的第二个 If 语句所在的位置(如果最大的是 None:)。另外,当您提到需要定义 asr 时,您是否建议在 try 和 except 语句上方将其定义为数值(例如,Astr=1?)或更具体的字符串(例如。Astr=Bob)?

标签: python-3.x loops syntax compiler-errors try-except


【解决方案1】:

这是简单的答案。它需要在一个函数中。创建一个函数,然后将 while 循环放入其中。

【讨论】:

    【解决方案2】:

    首先让我们注释我在您的原始代码中看到的一些机会:

    largest = None
    smallest = None
    fval = float(val)    ## "val" here has no definition and is a problem
    
    while True:
        val = input("Enter a number: ")
        fval = float(val)    ## if the user entered "done" this will be a problem here
        if val == "done":
            break
        print(fval)
    
        if smallest is None:
            smallest = fval
        elif fval > smallest :  ## you want to test if our new value is less than the current smallest one not greater than it
            fval = smallest
            return fval     ## "return" is only valid in the context of a method/function
    
        if largest is None:
            largest = fval
        elif fval > largest :
            largest = fval
            return fval     ## "return" is only valid in the context of a method/function
    
    try :
        int(astr)    ## "astr" here has no definition and is a problem
    
    except :
        print("invalid output")
    
    print("Minimum", smallest)
    print("Maximum", largest)
    

    那么,我们该如何清理一下...

    largest = None
    smallest = None
    
    while True:
        val = input("Enter a number: ")
    
        ##---------------------------
        ## Ask for numbers until the user gives us the string "done"
        ## If they do, we "break" out of this while loop
        ##---------------------------
        if val == "done":
            break
        ##---------------------------
    
        ##---------------------------
        ## try to get the input as an integer and if not an integer ignore it
        ## per our instructions by "continuing" to the next iteration of our while loop
        ##---------------------------
        try:
            fval = int(val)
        except ValueError as e:
            print(f"\"{val}\" is not a valid integer or the command \"done\"")
            continue
        ##---------------------------
    
        ##---------------------------
        ## update our tracking of smallest if appropriate
        ##---------------------------
        if smallest is None or fval < smallest:
            smallest = fval
        ##---------------------------
    
        ##---------------------------
        ## update our tracking of largest if appropriate
        ##---------------------------
        if largest is None or fval > largest:
            largest = fval
        ##---------------------------
    
    print(f"Minimum: {smallest}")
    print(f"Maximum: {largest}")
    

    【讨论】:

    • 非常感谢乔恩!我非常感谢您的所有帮助,并花时间告诉我哪里出了问题。
    • 嘿@DGB,我个人并不担心,但是如果有人在这里帮助你,那么识别它的方法是支持有用的答案,希望你能得到一个可以解决你的问题的答案您可以将其标记为“已接受的答案”。我相信本教程将引导您完成它。 stackoverflow.com/tour
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2017-12-11
    • 2011-02-11
    相关资源
    最近更新 更多