Python Assignments
  • Python Assignments
  • Operators Assignment
  • User Input Assignments
  • String Assignments
  • Conditional Statements - Assignments
  • Loops - Assignments
  • List Assignments
  • Function Practice Questions
  • File Handling Assignments
  • Miscellaneous Python Assignments
Powered by GitBook
On this page

Loops - Assignments

1.Try to Write your name 10 times with the help of while loop.

Solution

i = 1

while(i<=10):

print('David')

i = i + 1

2.Print a simple multiplication table of number given by user input.

Enter the number : 10
10
20
30
40
50
60
70
80
90
100
Solution

num = int(input('Enter the number : '))

i = 1

while(i<=10):

print(num * i)

i = i + 1

3. Do you know Factorial ? It is the product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24. Take a number from user and calculate factorial of a number.

Enter the number : 4

Factorial : 24

Solution

num = int(input('Enter the number : '))

i = 1

f = 1

while(i<=num):

f = f * i

i =i + 1

print(f)

4.Take 10 values from user and find out the sum and multiplication and average of the numbers.

Solution

i = 1

s = 0

p = 1

while(i<=10):

num = int(input('Enter the number : '))

s = s + i

p = p * i

print(f'Sum is : {s}')

print(f'Product is : {p}')

print(f'Average is {s/(i-1)}')

5.Create a game 'Guess the number' Game. Let us go through Rules one by one.

1.Computer will generate a Random Number.

2.Ask user to guess the number in 10 chances.

3.If user guesses it right , Score him accordingly like if user guesses in first chance score is 100 , in second chance score should be 90 and so on.

4.If guess number is greater than random number give him 'Hint : Choose a Lower Number' or less than random number give him 'hint : Choose a Higher Number' or if guess number is equal to random , No need to hint , just display score and end the game.

5.Also show how many chances are left.

6.if user could not guess the number, disclose the random number and end the game.

Guess the number : 60

Hint : Choose a Lower Number

---------------Number of Chances Left -------------->1

Guess the number : 50

Hint : Choose a Higher Number.

---------------Number of Chances Left -------------->2

Guess the number : 55

Hint : Choose a Higher Number.

---------------Number of Chances Left -------------->3

Guess the number : 58

You Won .

Score : 70

Solution

import random

random_number = random.randint(1,100)

print(random_number)

chances = 1

score = 110

while(chances<=10):

guess_number = int(input('Guess the number : '))

if(guess_number==random_number):

score = score - chances*10

print(f'You Won . Score : {score}')

break

elif(guess_number>random_number):

print('Hint : Choose a Lower Number')

else:

print('Hint : Choose a Higher Number.')

print(f'---------------Number of Chances Left -------->{chances}')

chances = chances + 1

else:

print(f'Sorry ! You lost . You ran out of your chances. Random number was : {random_number}')

6.Find all prime numbers between 1 to 2000.

Solution

for num in range(1,2000):

count = 0

for i in range(1,num):

if(num%i==0):

count = count + 1

if(count==1):

print(num)

7.Take a number from user and return sum of all digits of the number.

Enter The Number : 484

Sum of the digits : 16

Solution

number = int(input('Enter The Number : '))

s = 0

while(number>0):

rem = number%10

s = s + rem

number = number // 10

print('Sum of the digits : ',s)

8.Take integer inputs from user until he/she presses q ( Ask to press q to quit after every integer input ). Print average and product of all numbers.

Enter the number : 5

do you want to quit ? no

Enter the number : 10

do you want to quit ? no

Enter the number : 25

do you want to quit ? yes

Sum is 40

Product is 1250

Solution

s = 0

p = 1

while(True):

number = int(input('Enter the number : '))

s = s + number

p = p * number

q = input('do you want to quit ? ').lower()

if(q=='yes'):

break

print(f'Sum is {s}')

print(f'Product is {p}')

PreviousConditional Statements - AssignmentsNextList Assignments

Last updated 2 years ago