0

私はコーディングに比較的慣れていないので、ここで私を助けてください。コードは 5 行目までしか実行されません。このコードは完全にばかげているかもしれませんが、冗談を言ってください。

編集: 例外はありません。何も起こりません。1 と 2 のどちらかを選択するように求めた後、コードは停止します。

print('This program will tell you the area some shapes')
print('You can choose between...')
print('1. rectangle')
print('or')
print('2. triangle')

def shape():
    shape = int(input('What shape do you choose?'))

    if shape == 1: rectangle
    elif shape == 2: triangle
    else: print('ERROR: select either rectangle or triangle')

def rectangle():
    l = int(input('What is the length?'))
    w = int(input('What is the width?'))
    areaR=l*w
    print('The are is...')
    print(areaR)

def triangle():
    b = int(input('What is the base?'))
    h = int(input('What is the height?'))
    first=b*h
    areaT=.5*first
    print('The area is...')
    print(areaT)
4

2 に答える 2

11

あなたの問題は、コードを関数に入れたことですが、それらを決して呼び出さないことです。

関数を定義する場合:

def shape():
    ...

そのコードを実行するには、関数を呼び出す必要があります。

shape()

Python はコードを順番に実行することに注意してください。そのため、呼び出す前に関数を定義する必要があります。

また、関数を呼び出すには、引数を渡さない場合でも、常に括弧が必要になることに注意してください。

if shape == 1: rectangle

何もしません。あなたがしたいrectangle()

于 2012-06-22T17:31:45.463 に答える
0

より良いコーディング スタイルは、すべてを関数内に配置することです。

def display():
    print('This program will tell you the area some shapes')
    print('You can choose between...')
    print('1. rectangle')
    print('or')
    print('2. triangle')

def shape():
    shap = int(input('What shape do you choose?'))
    if shap == 1: rectangle()
    elif shap == 2: triangle()
    else:
        print('ERROR: select either rectangle or triangle')
        shape()


def rectangle():
    l = int(input('What is the length?'))
    w = int(input('What is the width?'))
    areaR=l*w
    print('The are is...')
    print(areaR)


def triangle():
    b = int(input('What is the base?'))
    h = int(input('What is the height?'))
    first=b*h
    areaT=.5*first
    print('The area is...')
    print(areaT)

if __name__=="__main__":
    display() #cal display to execute it 
    shape() #cal shape to execute it 
于 2012-06-22T17:52:21.107 に答える