Python Programming/Creating Python programs/Solutions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

< Back to Problems

1. Modify the hello.py program to say hello to a historical political leader (or to Ada Lovelace).

This is just a matter of replacing world with a different name, for example:

print('Hello, Ada!')

2. Change the program so that after the greeting, it asks, "How did you get here?".

This can be accomplished by adding a second print statement:

print('Hello, Ada!')
print('How did you get here?')

3. Re-write the original program to use two print statements: one for "Hello" and one for "world". The program should still only print out on one line.

This can be accomplished by passing the end parameter in the first print statement, and telling it to finish the output with a space (Python 3):

print ('Hello,', end=' ')
print ('world!')

When using Python 2.6 and up, you can use the ',' operator to prevent print from inserting a newline at the end.The ',' instead inserts a space at the end of the string to be printed. When using print as a function, the comma is placed outside the parentheses (Python 2.7.5).

print 'Hello,',
print 'world!'