我对编码比较陌生,所以请帮帮我.代码只会运行到第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)
Run Code Online (Sandbox Code Playgroud)
Gar*_*tty 11
您的问题是您已将代码放入函数中,但从不调用它们.
定义函数时:
def shape():
...
Run Code Online (Sandbox Code Playgroud)
要运行该代码,您需要调用该函数:
shape()
Run Code Online (Sandbox Code Playgroud)
请注意,Python按顺序运行代码 - 因此您需要在调用之前定义该函数.
另请注意,要调用函数,您总是需要括号,即使您没有传递任何参数,因此:
if shape == 1: rectangle
Run Code Online (Sandbox Code Playgroud)
什么都不做.你想要的rectangle().
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |