Domanda

Here is the example of the problem i am having it gives a duplicate of what i have entered for raw_input how do i access the value of raw_input and use it as a parameter of the function any one can help? i tried many times to amend it but didn't see any light of it i kind of know that the code must have treated the value as string but because i am new to python i don't have the necessary knowledge to solve it and it would be appreciated to have a full detailed how to. Inculcate me :)

def hotel_cost(nights):
    return nights*140

a=raw_input("please enter number")
print hotel_cost(a)

thank you to all that contribute :)

È stato utile?

Soluzione

raw_input returns a string, you should convert it to an integer using int():

a = int(raw_input("please enter number: "))

Example:

>>> "12"*10             # This is what your code is doing
'12121212121212121212'

>>> int("12")*10        # Expected output
120

Altri suggerimenti

raw_input will return a string so you have to do the convertion yourself. Or you can use input in 2.x. It equals eval(raw_input(prompt)).

a=int(raw_input("please enter number"))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top