[Tutor] Python input function not working in 2.7 version

Dave Angel d at davea.name
Wed Oct 24 13:33:11 CEST 2012


On 10/24/2012 07:15 AM, Morten Engvoldsen wrote:
> Hi,

Hi.  Welcome to the list.

> I am facing issue with input() of Python 2.7. When i run the program it
> doesn't display any line to take user input . Below is the code:
>
> def user_input()
Need a trailing colon here.
>    fat_grams = input("How many grams of fat are in one serving? ")
>    total_calories = input("How many total calories are in one serving? ")
>    print("A food product having {0} fat grams and {1} total
> calories",fat_grams,
> total_calories);
>    return;
>
> def main():
>    if __name__ == "__main__": main()

That "if" line needs to be at top-level.  Once you're inside main(),
it's a little late to decide whether to call main().

def user_input():
    ....

def main():
    user_input()

if __name__ == "__main__":
    main()



>
> Also i would like to display fat_grams where {0} place holder is there and
> total_calories where {1} place holder is there.
> That is if :
> fat_grams = 3
> total_calories = 270
>
> Output should be: A food product having 3 fat grams and 270 total calories.
>
> But the above print function doesn't display the out put in same way. I am
> new to Python and i appreciate your help in advance.

Easiest way to get that is to use the format() method of str.

msg = "A sentence has {0} and {1}".format(arg1, arg2)

Then you can print msg.

> Look forward to hear from your team soon and have a nice day.

Actually, we're all volunteers here, and not part of a team.  More like
a community.

> Regards,
> Morten
>
>


-- 

DaveA



More information about the Tutor mailing list