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

    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Simple Python Task (need some help please!)


    I'm trying to get my code to print a message for the user when they enter an integer value and an error message such as ("Please enter a whole integer value of eggs!") when they enter a non-integer value. This is supposed to be a basic python exercise, I've spent Friday and all of the weekend trying to crack it.


    My code so far is this:

    x = input("Please enter the number of eggs in the basket: ")
    str1 = ("There are ")
    str2 = x
    str3 = (" eggs in the basket.")
    if(type(x) == int):
    print (str1+str2+str3)
    else:
    print("Please enter a whole integer number of eggs!")


    Any suggestions would be appreciated!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0
    The way it should run if someone enters a non-integer value:
    "How many eggs are there in the basket? 3.5
    Please enter an integer!"
    I'm also using Python 3.2
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    510
    Rep Power
    33
    The "standard" way using an infinite loop and function. This assumes Python 3.X so input returns a string. In Python 2.X input returns a number
    Code:
    def get_an_int():
        while True:  ## infinite loop
            x = input("Please enter the number of eggs in the basket: ")
            try:
                return int(x)
            except:
                print("please enter an integer only")
    
    y=get_an_int()
    print(y, type(y))
    Last edited by dwblas; February 17th, 2014 at 03:22 PM.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481

    debugging advice


    I suggest that printing the data type along with a value immensly helps understanding python problems. You can substitute
    dbg(var)
    many places in your program that you have
    var
    Code:
    '''
        $ echo 3 | python p.py
        Please enter the number of eggs in the basket: x value and type 3 <class 'str'>
        str2 value and type 3 <class 'str'>
        Please enter a whole integer number of eggs!
    '''
    
    def dbg(value, title = 'dbg report'):
        print(title,'value and type',value,type(value))
        return value
    
    
    x = input("Please enter the number of eggs in the basket: ")
    str1 = ("There are ")
    str2 = dbg(x,'x')
    dbg(str2,'str2')
    str3 = (" eggs in the basket.")
    if(type(x) == int):
        print (str1+str2+str3)
    else:
        print("Please enter a whole integer number of eggs!")
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo