Python用户输入和打印

标签 python

chargePerDay = 30.0
feeEachMile = 0.5
fuelPerGallon = 1.5

print("How many users are there?")
numberOfUsers = input()

for i in range(numberOfUsers):
    print('For user ' + i)

    print("Number of days")
    numberOfDays = input()

    print("Number of miles")
    numberOfMiles = input()

    totalRentalFee = (chargePerDay * float(numberOfDays)) + (feeEachMile * float(numberOfMiles))
    print('Hi user ' + i + ' Your total rental fee is ' + str(totalRentalFee))

当我运行它时它正在打印 对于范围内的我(用户数): TypeError: 'str' 对象不能解释为整数

这是怎么回事??请帮忙!

最佳答案

你输入的是一个字符串,你需要把它转换成一个整数:

numberOfUsers = int(input())
# ... rest of code

关于Python用户输入和打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471427/

相关文章:

Python : if any(x in y), 大小写和重音不敏感?

python - 让cProf只给出10个最耗时的任务或者按照时间倒序对它们进行排序

python - 从按钮将数据输入到 Entry 小部件

python - 尝试通过计算列表列表中的出现次数来添加到字典值中(Python)

python - Pandas .unique() TypeError : unhashable type: 'list'

python - PIL : make image transparent on a percent scale

Python:GIL 上下文——切换

python - 为什么使用 python 旋转在 blender 2.6 中不起作用?

python - 如何判断 Python 模块是否是命名空间模块

python - OSX 上 PyBrain 上的 libsvm 用于 SVM