#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1
    Rep Power
    0

    Question Easy question from a beginner.


    Quick version:
    Whats wrong with this code?

    Long version:
    I'm just messing with some very basic coding for fun and I wanted to make one of those programs that take some personal information and use it to illegitimately say something about the user. Specifically, I am trying to use the number of characters in the name input to multiply together with the age input and then be output as 'the users IQ', I don't know what I'm doing wrong though. Here is my coding:

    print ("Tell me your name and age, then I will calculate your IQ.")
    name=input ("Name:")
    age=input ("Age:")
    print ("calculating...")
    namenum= len(name)
    namenumber= int(namenum)
    print (namenumber*age)
    input ("Press enter to exit. ")

    (Also, I know that the answer to this is out there somewhere, but I don't want to have to go hunting for ages.)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    162
    Rep Power
    18
    When you use the input function, python will try to interpret the input. So when you enter a string when the program prompts for Name, and you enter a string (say, John) without quotes, python will look for an already defined variable (or module) John; and will throw an error. So you have to enter "John" (with the quotes) to indicate python that you're entering a string. The same goes with numbers. If you enter 35 as age (without quotes), it will be interpreted as an int.

    This can be avoided by using the raw_input function which will always interpret the entered text as a string.

    Code:
    [user@host ~]$ ./test.py
    Tell me your name and age, then I will calculate your IQ.
    Name:"John"
    Age:35
    calculating...
    140
    Press enter to exit.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0
    agree with noobie1000.

    Just take care while you enter name put it in single or double quote as it is string.

    like

    "bhawani"

    >>> name=input ("Name:")
    Name:'bhawani'
    >>> age=input ("Age:")
    Age:2
    >>> namenum= len(name)
    >>> namenumber= int(namenum)
    >>> print (namenumber*age)
    14
    >>> input ("Press enter to exit. ")
    Press enter to exit.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Write your code in a python version independent manner.
    Read from sys.stdin
    Write to sys.stdout
    Avoid input and raw_input
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo