1
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))

When I run it it is printing for i in range(numberOfUsers): TypeError: 'str' object cannot be interpreted as an integer

What is wrong with this?? Please help!

0

4 Answers 4

3

You're input is coming in as a string, you need to convert it to an integer:

numberOfUsers = int(input())
# ... rest of code
Sign up to request clarification or add additional context in comments.

Comments

1

input returns a string. range wants an integer.

Change range(numberOfUsers) to range(int(numberOfUsers))

The next error is in the following lines when you try to concatenate strings with the integer i. Use str(i) in the calls to print.

Comments

0

Cast to int.

numberOfUsers = int(input())

Python 2's input() evaluates automatically, whereas Python 3 got rid of 2's input() and replaced it with the equivalent of raw_input() which takes input as a string.

Comments

0

Your problem is that you are using integer data objects instead of strings. To convert them, use this code:

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 ' + str(i))

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.