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

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

使用python求解三角函數(shù)時(shí)出錯(cuò)

使用python求解三角函數(shù)時(shí)出錯(cuò)

一只甜甜圈 2024-01-04 15:23:43
import mathnumber = input('Your Number: ')ways = input('sin/cos/tan: ')try:    problem = ways(number)    answer = math.problem    print(f'The value of {ways} of {number} is: {problem}')這是我的代碼。我想使用 python 中的數(shù)學(xué)模塊來(lái)求解三角函數(shù),但每次運(yùn)行它時(shí)都會(huì)出現(xiàn)錯(cuò)誤SyntaxError: unexpected EOF while parsing
查看完整描述

3 回答

?
紫衣仙女

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

我試圖try except通過(guò)使用稍微不同的邏輯來(lái)測(cè)試輸入的有效性來(lái)完全避免您的問(wèn)題。


另外,我使用了在運(yùn)行時(shí)將字符串映射到函數(shù)的規(guī)范解決方案,即使用映射(在 Python 中,a dict)。


這是我的解決方案,經(jīng)過(guò)一些測(cè)試運(yùn)行。


In [6]: import math 

   ...: trigs = {'sin':math.sin, 'cos':math.cos, 'tan':math.tan} 

   ...: while True: 

   ...:     try:

   ...:         number = input('Your Number: ')

   ...:         fnumber = float(number) 

   ...:         break 

   ...:     except ValueError: 

   ...:         print('You input a non-valid floating point number.\nPlease try again') 

   ...:         continue 

   ...: while True: 

   ...:     trig = input('sin/cos/tan: ') 

   ...:     if trig in trigs: break 

   ...:     print('You input a non-valid trig function.\nPlease try again') 

   ...:  

   ...: print(f'The value of {trig} of {number} is: {trigs[trig](fnumber)}')              

Your Number: ret

You input a non-valid floating point number.

Please try again

Your Number: 1.57

sin/cos/tan: ert

You input a non-valid trig function.

Please try again

sin/cos/tan: tan

The value of tan of 1.57 is: 1255.7655915007897


In [7]:         


查看完整回答
反對(duì) 回復(fù) 2024-01-04
?
30秒到達(dá)戰(zhàn)場(chǎng)

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

你應(yīng)該有一個(gè)except塊,它至少可以處理錯(cuò)誤pass

ways實(shí)際上是函數(shù),采用不同的輸入

import math


number = input('Your Number: ')

ways_ = input('sin/cos/tan: ')


try:

    problem = ways(number)

    answer = math.problem

    print(f'The value of {ways} of {number} is: {number}')

except:

    pass

    # anything else?? handle errors??


查看完整回答
反對(duì) 回復(fù) 2024-01-04
?
波斯汪

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

您需要添加except用于處理異常的塊,以防代碼中出現(xiàn)問(wèn)題。


您可以編寫(xiě)這樣的代碼來(lái)實(shí)現(xiàn)您想要的任務(wù):


import math

number = input('Your Number: ')

ways = input('sin/cos/tan: ')


def math_func(num, type_func):

    func = {'sin': lambda: math.sin(num),

            'cos': lambda: math.cos(num),

            'tan': lambda: math.tan(num)}

    return func.get(type_func)()


try:

    answer = math_func(float(number), ways)

    print(f'The value of {ways} of {number} is: {answer}')

except:

    pass


查看完整回答
反對(duì) 回復(fù) 2024-01-04
  • 3 回答
  • 0 關(guān)注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報(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)