[Tutor] stuck on null values
Joel Goldstick
joel.goldstick at gmail.com
Fri Mar 16 15:44:34 CET 2012
On Thu, Mar 15, 2012 at 5:45 PM, ADRIAN KELLY <kellyadrian at hotmail.com> wrote:
>
>
>
> Adrian Kelly
> 1 Bramble Close
> Baylough
> Athlone
> County Westmeath
>
> 0879495663
>
>
>> Date: Thu, 15 Mar 2012 17:38:52 -0400
>> Subject: Re: [Tutor] stuck on null values
>> From: joel.goldstick at gmail.com
>> To: kellyadrian at hotmail.com
>> CC: tutor at python.org
>
>>
>> On Thu, Mar 15, 2012 at 5:24 PM, ADRIAN KELLY <kellyadrian at hotmail.com>
>> wrote:
>> > Please can anyone tell me how to solve the problem i am having here, i
>> > am
>> > trying to loop if the value is left blank by the user
>> > but how can i then use the value and multiply it. e.g. while
>> > number_child==""............ i want to multiply the input i
>> > get.........???
>> >
>> > def main():
>> > print """
>> > ________________________________________________________
>> >
>> > Welcome to the Travel Kiosk
>> > ________________________________________________________
>> > """
>> >
>> > adult=15
>> > child=5
>> > firstname=raw_input("Please enter your firstname: ")
>> > lastname=raw_input ("Please enter your lastname: ")
>> > number_child=raw_input("Enter the number of kids: ")
>> > number_adults=raw_input("Enter the number of adults: ")
>> > while number_child=="":
>> > number_child=raw_input("Enter the number of kids: ")
>> > price=child*number_child
>> >
>> >
>> > print """
>> > _______________________________________________________
>> >
>> > Thank you, the Price will be: """,price
>> > print"""
>> > _______________________________________________________
>> > """
>> >
>> > main()
>> >
>> >
>> >
>> >
>> >
>> >
>> > Adrian Kelly
>> > 1 Bramble Close
>> > Baylough
>> > Athlone
>> > County Westmeath
>> >
>> > 0879495663
>> >
>> > _______________________________________________
>> > Tutor maillist - Tutor at python.org
>> > To unsubscribe or change subscription options:
>> > http://mail.python.org/mailman/listinfo/tutor
>> >
>> raw_input returns the values the user types as a string. In python if
>> you have 3 * 'bob' the result will be "bobbobbob"
>>
>> So you need to turn the number_child into a number like so:
>> int(number_child)
>>
>> You will need to do the same thing for the number of adults.
>>
>> That will get you started, but if your user types in something that
>> isn't a number, your program will fail. See if you can get it to work
>> for good data and then come back with what happens when you type in
>> other than integers.
>>
>> Also, when your program fails, it prints out what is called a
>> traceback. When you get this situation, be sure to post the traceback
>> with your problem. It helps figure out what went wrong
>>
>>
>> --
>> Joel Goldstick
>
> ...............................................................................................................
> adrian replied,
> i tried it with integers earlier and then i couldn't use while
> number_children=="":
>
>
First, don't reply to the last responder. Reply to the list
Take a look at this:
while True:
num = raw_input("give me an integer: ")
try:
n = int(num)
break
except:
print "Sorry, that wasn't and integer!"
pass
While True will keep doing forever the block underneath it
try: is how python checks to see if something causes an except. An
exception is something that isn't what you want to have happen for
your code to continue happily
if n can't be converted to an int, it raises an exception, and
immediately jumps to the code at 'except:
However, if n=int(num) is happy, break happens which will get you out
of the while True loop
--
Joel Goldstick
More information about the Tutor
mailing list