因练习回合功能代码而陷入错误

时间:2018-09-24 04:42:32

标签: python

使用round()函数以及打印和输入的基本代码

fullName= input("Enter your first and last name: ")    
print("Hello, " + fullName + " nice to meet you")    
Age= input("How old are you: ")    
print("Wow, you're actually " + Age + " years old!? ")    
print("I will now move onto grabbing information and round functions")    
milesdrivenA= input("How many miles did you drive to point A? ")    
milesdrivenB= input("How many miles did you drive to point B? ")    
gallonsusedA= input("How many gallons did you use to point A? ")    
gallonsusedB= input("How many gallons did you use to point B? ")    
totalMiles= str(milesdrivenA + milesdrivenB)    
totalGallons= str(gallonsusedA + gallonsusedB)    
mpg= round(totalMiles / totalGallons, 2)

错误:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

请原谅所有额外的内容,例如“哇,你今年几岁”,老实说,它是一种额外的练习方式(我编写的代码越多,我对自己的工作就会越熟悉)。

现在在上面的代码中我的问题,我在做什么错?可能是一个非常简单的答案,但我确实是超级初学者,所以我不知道我到底缺少什么。我已经尝试过将totalMiles = ^的内容更改为totalMiles= str( int(milesdrivenA + milesdrivenB ))之类的东西。感谢您的提前帮助!

1 个答案:

答案 0 :(得分:0)

在这里,您将free转换为int,然后尝试对它们进行数学运算,使它们str s进行除法

@DYZ表示我们可以直接进入int,永远不要以int开头

str
totalMiles= int(milesdrivenA + milesdrivenB)

totalGallons= int(gallonsusedA + gallonsusedB)

mpg= round(totalMiles / totalGallons, 2)