Vous êtes sur la page 1sur 3

1. Write a program that prints 'Hello World' to the screen.

print("Hello World")

2. Write a program that asks the user for their name and greets them with their
name.
print("Hi there! May I ask your name?")
name = input()
print("Nice to meet you %s!"%name)

3. Modify the previous program such that only the users Alice and Bob are greeted
with their names.
print("Hi there! May I ask your name?")
name = input()
if name == 'Alice':
print("Nice to meet you %s!"%name)
elif name == 'Bob':
print("Nice to meet you %s!"%name)
else:
print("I’m sorry stranger but I don’t know you.")

4. Write a program that asks the user for a number n and prints the sum of the
numbers 1 to n.
print("Can you give me any number? And I will give you the sum from 1 to n.")
n = int(input())
i = 1
while i <= n:
sum = n + i
print(I,””+””,n,”=”,sum)
i += 1

5. Modify the previous program such that only multiples of three or five are
considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17.
print("Multiples of 3 and 5 are considered in the sum for n = 17")
n = 17
nums = [3,5]
result = 0
for num in nums:
for i in range(1,n):
if num*1 <= n:
print(result)
result += num*1

6. Write a program that asks the user for a number n and gives them the
possibility to choose between computing the sum and computing the product of
1,…,n.

print("Enter any number: ")


n = int(input())
print("Press (P) if you want to calculate the product, and press (S) for sum:")
ans = input()
ans = ans.capitalize()
print()
while ans == 'P':
s = 1
while s <= n:
prod = n * s
print(s,"x",n,"=",prod)
s += 1
break
while ans == 'S':
s = 1
while s <= n:
sum = n + s
print(s,"+",n,"=",sum)
s += 1
break

7. Write a program that prints a multiplication table for numbers up to 12.


n = 12
s = 1
while s <= n:
prod = n * s
print(s, "x", n, "=", prod)
s += 1

8. Write a guessing game where the user has to guess a secret number. After every
guess the program tells the user whether their number was too large or too small.
At the end the number of tries needed should be printed. It counts only as one try
if they input the same number multiple times consecutively.
import random
print('Hello! What is your name?')
myName = input()
myName = myName.capitalize()
print("Hey %s,"%myName,"can you give me any number?")
maxn = int(input())
n = random.randint(1,maxn)
print("Now "+myName+",try to guess a number between 1 and %s."%maxn)
print("Note: I will only give you 10 tries.")
print()
guess = None
guess_counter = 1
guessList = []
guessList.append(guess)
while guess != n:
guess_counter += 1
guess = int(input('Your Guess is:'))
if guess > n:
print("Too high")
print("Guess",guess_counter,"out of 10.")
print()
if guess not in guessList:
guessList.append(guess)
elif guess < n:
print("Too low")
print("Guess", guess_counter, "out of 10.")
print()
if guess not in guessList:
guessList.append(guess)
elif guess not in guessList:
guessList.append(guess)
print("Congratulations, you guessed it right! The answer was {}! It took
you {} tries.".format(n, len(guessList)))
elif guess_counter == 10:
print("You run out of tries!")
break

9. Write a function that takes a list of strings an prints them, one per line, in
a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"]
gets printed as:
def print_in_a_frame(*words):
size = max(len(words) for word in words)
print("*" * (size + 4))
for word in words:
print('* {:<{}} *'.format(word, size))
print('*' * (size + 4))
print_in_a_frame("Hello", "World", "in", "a", "frame")