[Tutor] adding quotation marks around variables
Brian van den Broek
bvande at po-box.mcgill.ca
Mon Jul 11 00:20:08 CEST 2005
Robert said unto the world upon 10/07/2005 17:31:
> Hello all, I am a college student and I am currently working on a
> two numbers program for our class, The purpose of this program is
> to take user input and do some math functions.
>
> I have figured out how to do the math but I need to display these
> two variables with quotation marks around them.
>
> Also I need to add these two variables together and display the
> hexadecimal of these two variables???
>
> Also how do you display wheather one variable is greater than or
> less than or equal to the other variable?
>
>
> Below is an example of the items i need help with?
>
>
>
>
>
>
> The two numbers were "X" and "Y."
>
> The first number was (< > =) the second number.
>
> The hexadecimal sum of the two numbers is XXX.
>
> Any help with this would be gratly appreciated! Thanks Robert
>
Hi Robert,
for displaying strings with variable content, string formatting is a
real boon. See <http://docs.python.org/lib/typesseq-strings.html> for
details.
But here is a bit to give you the idea:
>>> def silly_example():
name = raw_input('What is your name?\n')
if name[0].isalpha():
if name[0].lower() in ('aeiou'):
char_type = 'a vowel'
else:
char_type = 'a consonant'
else:
char_type = "something other than a letter"
print "Hello, %s. Your name begins with %s." %(name, char_type)
>>> silly_example()
What is your name?
Brian
Hello, Brian. Your name begins with a consonant.
>>> silly_example()
What is your name?
Alberto
Hello, Alberto. Your name begins with a vowel.
>>> silly_example()
What is your name?
42
Hello, 42. Your name begins with something other than a letter.
>>>
See if you can get somewhere with these ideas. If you get stuck, post
some code, and someone will be glad to help more.
Best,
Brian vdB
More information about the Tutor
mailing list