Uploaded by Waleed Naveed

Loop Task, 57789

advertisement
MUHAMMAD WALEED NAVEED
57789
INTRO TO PYTHON
CLASS TASK-LOOP
Task-1
Write a Python program to guess a number between 1 to 9.
Note : User is prompted to enter a guess. If the user guesses wrong then the prompt appears
again until the guess is correct, on successful guess, user will get a "Well guessed!" message, and
the program will exit.
Answer 1
import random
target_num, guess_num = random.randint(1, 10), 0
while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until you
get it right : '))
print('Well guessed!')
Task-2
Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
Answer 2
for x in range(6):
if (x == 3 or x==6):
continue
print(x,end=' ')
print("\n")
Task-3
Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will
return 20.
Answer 3
def sum(x, y):
sum = x + y
if sum in range(15, 20):
return 20
else:
return sum
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
Taks-4
Using a for loop and .append() method append each item with a Dr. prefix to the lst.
Example lst1=[“John", “Vick", "Alex", "Dock"]
Output:
Dr John Dr vick Dr Alex Dr Dock
Exercises
Growing Strength







Make a variable called strength, and set its initial value to 5.
Print a message reporting the player's strength.
Set up a while loop that runs until the player's strength increases to a value such as 10.
Inside the while loop, print a message that reports the player's current strength.
Inside the while loop, write a statement that increases the player's strength.
Outside the while loop, print a message reporting that the player has grown too strong,
and that they have moved up to a new level of the game.
Bonus: Play around with different cutoff levels for the value of strength, and play around
with different ways to increase the strength value within the while loop.
Exercises
Game Preferences





Make a list that includes 3 or 4 games that you like to play.
Print a statement that tells the user what games you like.
Ask the user to tell you a game they like, and store the game in a variable such as
new_game.
Add the user's game to your list.
Print a new statement that lists all of the games that we like to play (we means you and
your user).
Download