第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

值錯(cuò)誤和零除錯(cuò)誤

值錯(cuò)誤和零除錯(cuò)誤

慕田峪9158850 2023-10-26 10:19:08
我正在嘗試執(zhí)行計(jì)算 ((x/y) + z) 并解釋 2 個(gè)錯(cuò)誤:ValueError和ZeroDivisionError.但是,我很難理解如何將這兩個(gè)錯(cuò)誤編碼到我的程序中。我想我已經(jīng)ValueError明白了,但還沒有ZeroDivisionError。這是我到目前為止所擁有的。抱歉,有點(diǎn)亂 rn....user_input = (input("Enter three numbers separated by a space: ")).split()x = int(user_input[0])y = int(user_input[1])z = int(user_input[2])  def calculate(x, y, z):    '''calculate (x/y)+z'''    c = ((x/y) + z) if y != 0 else print("Second input value cannot be 0")    return ctry:    input_values_str = str(user_input)    c = ((x/y) + z)    for val in input_values_str:        if len(user_input) == 3:            print("Correct number of values.")        else:            print("Incorrect number of values entered.")except ValueError:    print(user_input," is not valid input.")except ZeroDivisionError:    y = 0    print("Second value cannot be 0")print("Formula: ({}/{}) + {} = {}".format(x, y, z, calculate(x, y, z)))
查看完整描述

4 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

恭喜你學(xué)習(xí)了Python!我認(rèn)為你的代碼有點(diǎn)混亂。我將發(fā)布您的版本以及我的一些評(píng)論,然后我將發(fā)布一個(gè)清理版本,我認(rèn)為它可以更好地實(shí)現(xiàn)您想要做的事情。希望它具有指導(dǎo)意義。有更優(yōu)雅的方法可以做到這一點(diǎn),但我試圖保留你的結(jié)構(gòu)盡可能有意義。


您的代碼:


user_input = (input("Enter three numbers separated by a space: ")).split()

x = int(user_input[0])

y = int(user_input[1])

z = int(user_input[2])


def calculate(x, y, z):

    c = ((x/y) + z) if y != 0 else print("Second input value cannot be 0")  #Why do you need a ZeroDivisonError check below when you check for Zero here?

return c


try:

    input_values_str = str(user_input) # why are you taking an input here? Did you take the input above? And why convert it back into a string?

    c = ((x/y) + z)

    for val in input_values_str: # Why are you iterating through string? You're basically checking 3 times if the length of your string is 3 characters

        if len(user_input) == 3:

            print("Correct number of values.")

        else:

            print("Incorrect number of values entered.")


except ValueError:

    print(user_input," is not valid input.")


except ZeroDivisionError:

    y = 0 # why set y back to zero in the event oa ZeroDivisonError? Won't that just create a ZeroDivison Error? Might make more sense to get a new input from the user

    print("Second value cannot be 0")

print("Formula: ({}/{}) + {} = {}".format(x, y, z, calculate(x, y, z))) # if the objective is to only print something, why not do it in the calculate function? Then we can contain your ZeroDivisionError in their and request a new y

我的代碼:


def calculate(x, y, z):

    try:

        solution = (x/y) + z

        print(f"Formula: ({x}/{y}) + {z} = {solution}")


    except ZeroDivisionError:

        y = int(input("2nd value cannot be a Zero. Please input a new y value: "))

        calculate(x, y, z)


try:

    user_input = (input("Enter three numbers separated by a space: ")).split()


    while len(user_input) != 3:

        print("Incorrect number of values entered.")

        user_input = (input("Enter three numbers separated by a space: ")).split()


    x = int(user_input[0])

    y = int(user_input[1])

    z = int(user_input[2])


    calculate(x, y, z)


except ValueError:

    print(user_input," is not valid input.")


查看完整回答
反對(duì) 回復(fù) 2023-10-26
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

我只是調(diào)整了您的代碼以滿足您的期望,并且易于理解。


user_input = (input("Enter three numbers separated by a space: ")).split()

x = int(user_input[0])

y = int(user_input[1])

z = int(user_input[2])


try:

    c = ((x/y) + z) if y != 0 else print("Second input value cannot be 0")

except ValueError:

    print(user_input," is not valid input.")

print("Formula: ({}/{}) + {} = {}".format(x, y, z, c))


查看完整回答
反對(duì) 回復(fù) 2023-10-26
?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

我根本不明白為什么你需要 try/ except/ except 塊。y=0您已經(jīng)在您的方法中進(jìn)行了檢查calculate,并且您已經(jīng)知道您有有效的輸入,否則 和split強(qiáng)制轉(zhuǎn)換int()將會(huì)失敗。其中的循環(huán)for一遍又一遍地打印相同的輸出字符串。如果你把所有這些都去掉,你的代碼似乎工作正常


try:

    file_name = open('/tmp/data.txt', 'r')

except FileNotFoundError:

    print("File could not be found. Please check spelling of file name!")

    sys.exit()


#Read lines in file

Lines = file_name.read().splitlines()


user_input = (input("Enter three numbers separated by a space: ")).split()

x = int(user_input[0])

y = int(user_input[1])

z = int(user_input[2])


def calculate(x, y, z):

    '''calculate (x/y)+z'''

    c = ((x / y) + z) if y != 0 else print("Second input value cannot be 0")

    return c


print("Formula: ({}/{}) + {} = {}".format(x, y, z, calculate(x, y, z)))


查看完整回答
反對(duì) 回復(fù) 2023-10-26
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

您引發(fā)了一個(gè)異常來(lái)查看 y == 0 是否,但第一個(gè)值 (x) 也不能為 0。并且您需要將 2 個(gè) if 語(yǔ)句放在 c = ... 之前


嘗試這個(gè):


user_input = (input("Enter three numbers separated by a space: ")).split()

x = int(user_input[0])

y = int(user_input[1])

z = int(user_input[2])


def calculate(x, y, z):

    '''calculate (x/y)+z'''

    if y == 0 or x == y:

        print("Invalid Input")

    else:

        c = ((x/y) + z)

        return c


try:

    input_values_str = str(user_input)

    c = ((x/y) + z)

    for val in input_values_str:

        if len(user_input) == 3:

            print("Correct number of values.")

        else:

            print("Incorrect number of values entered.")


except ValueError:

    print(user_input," is not valid input.")


except ZeroDivisionError:

    y = 0

    x = 0

    print("Second value cannot be 0. First Value cannot be 0")

    print("Formula: ({}/{}) + {} = {}".format(x, y, z, calculate(x, y, z)))


查看完整回答
反對(duì) 回復(fù) 2023-10-26
  • 4 回答
  • 0 關(guān)注
  • 230 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)