python 使用input()获取值

5vf7fwbs  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(171)

我正在试着做一个程序,它可以计算长度和宽度,并打印出每一个观察结果。
我有这个:

length = 0 
 width = 0

def GetValues():
    print("Lengt", i+1)
    length = int(input())
    print("width(m) ", i+1)
    width = int(input())
    return length,width

print("How many times")
Times = int(input())

for i in range(Times):
   GetValues()
   Area = length(i)*width(i)
   print('The area :', Area)

但我得到TypeError:“int”对象不可调用

oxcyiej7

oxcyiej71#

解决方案如下

length = 0 
width = 0

def GetValues():
    print("Lengt", i+1)
    length = int(input())
    print("width(m) ", i+1)
    width = int(input())
    return length,width

print("How many times")
Times = int(input())

for i in range(Times):
   length, width = GetValues() # extract return values from function
   Area = length*width # just multiply them
   print('The area :', Area)

相关问题