0% found this document useful (0 votes)
7 views3 pages

Assignment_ Working With Loops

The assignment focuses on practicing for and while loops in Python through four parts. Part 1 requires calculating the sum of numbers from 1 to a user-inputted positive integer, Part 2 involves printing the multiplication table for a given number, Part 3 is a guessing game where the user guesses a randomly chosen number, and Part 4 entails calculating the factorial of a user-inputted positive integer. Each part includes examples of expected input and output.

Uploaded by

Somebody's Bae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Assignment_ Working With Loops

The assignment focuses on practicing for and while loops in Python through four parts. Part 1 requires calculating the sum of numbers from 1 to a user-inputted positive integer, Part 2 involves printing the multiplication table for a given number, Part 3 is a guessing game where the user guesses a randomly chosen number, and Part 4 entails calculating the factorial of a user-inputted positive integer. Each part includes examples of expected input and output.

Uploaded by

Somebody's Bae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment: Working with Loops

Objective:

Practice using for and while loops in Python to solve problems.

Part 1: Sum of Numbers (For Loop)

Write a program that:

1. Asks the user to input a positive integer, n.


2. Uses a for loop to calculate and print the sum of all numbers from 1 to n.

Example:

# Sample input
Enter a positive integer: 5

# Expected output
The sum of numbers from 1 to 5 is: 15

Part 2: Multiplication Table (For Loop)

Write a program that:

1. Asks the user to input a number.


2. Prints the multiplication table for that number from 1 to 10 using a for loop.

Example:

# Sample input
Enter a number: 3

# Expected output
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Part 3: Guess the Number (While Loop)

Write a program that:

1. Picks a random number between 1 and 10 (you can use random.randint).


2. Asks the user to guess the number.
3. Uses a while loop to keep asking the user for guesses until they guess the correct
number.
4. When the guess is correct, print a congratulatory message and the number of attempts it
took.

Hint: You can use import random to generate a random number.

Example:

# Sample output (user guesses)


Guess the number: 5
Too low! Try again.
Guess the number: 8
Too high! Try again.
Guess the number: 7
Congratulations! You guessed the correct number in 3 attempts.

Part 4: Factorial (While Loop)

Write a program that:

1. Asks the user to input a positive integer.


2. Uses a while loop to calculate and print the factorial of the number.

Example:
# Sample input
Enter a positive integer: 5

# Expected output
The factorial of 5 is: 120

This assignment will help you apply the knowledge of for and while loops in practical scenarios.

You might also like