初学Python新手问题

2025年03月18日 18:50
有4个网友回答
网友(1):

height = eval(input("Please enter the height:"))
width = eval(input("Please enter the width:"))
area = height * width
print ("The area is", area)

python2.x的input(prompt)相当于eval(raw_input(prompt)).

python3.x的input(prompt)则基本等价于raw_input(prompt),所以返回的是一个字符串,你要不eval他,会自动变成一个整形,要不直接强制转换为整形如
height = int(input("Please enter the height:"))

网友(2):

input()函数输入后事字符串,字符串不能有*运算,你需要把height和width的值都改成数,你可以通过导入模块,然后运用字符串转数字的函数atoi()来转换,3.1版本中做了一些修改,以前atoi是在string模块中,3.1的记不住了,你查一下如果是2.5版本,就可以这样写
import string
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area=string.atoi(‘height’)*string.atoi(‘width’)
我说了,3.1版本的atoi函数不在string模块中,所以你编写的时候上网查一下这个函数在那个模块中,然后用它替换string就行了
当然这个事比较麻烦的做法,简单一点的是直接转换
Int(‘height’)*int(‘width’)
就行了,比较简单

网友(3):

area = int(height)*int(width)

网友(4):

input()
import string
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area=string.atoi(‘height’)*string.atoi(‘width’)
我是对的!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1