TypeError: cannot concatenate 'str' and 'int' objects Can someone please help a newbie with their code?

后端 未结 2 2026
我在风中等你
我在风中等你 2021-01-28 08:32

Any help is appreciated, also any big flaws or something you see in the way im formatting or something basic, please point it out. Thanks!

day = raw_input(\"         


        
相关标签:
2条回答
  • 2021-01-28 09:15

    First thing to understand is that raw_input returns a string, so there's no need to cast the result to a string afterwards.

    What you want (I think) is to cast day to an int, so you need to change the top part.

    day = raw_input("How many days?")
    location = raw_input("Where to?")
    days = int(day)
    spendingMoney = 100
    

    In your original code, days was a string, and so you were trying to add a string to and integer (which raised the error).

    Multiplying a string by an integer is perfectly valid, as it simply repeats the original string several times over.

    print 'foobar' * 5
    # foobarfoobarfoobarfoobarfoobar
    
    0 讨论(0)
  • 2021-01-28 09:22

    the problem is that days is a string.

    when you do

    return 140 * days
    

    it actually multiples your string to 140. so if days == "5" you will have "555555555555555555..." (140 characters)

    you want to operate with integers so do days = int(day) instead

    0 讨论(0)
提交回复
热议问题