-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -495,6 +495,7 @@ [email protected] | |
## 5. Conditional Iteration | ||
|
||
1. Watch the video on using the **while** statement: | ||
|
||
<https://youtu.be/APWy6Pc83gE?feature=shared> | ||
|
||
|
||
|
@@ -519,33 +520,22 @@ [email protected] | |
1. | ||
```python | ||
### | ||
# | ||
# Displays a greeting | ||
# | ||
name = '' | ||
|
||
while name == '': | ||
name = input('Enter your name: ') | ||
|
||
print(f'Hello {name}') | ||
``` | ||
|
||
1. The following program sums the numbers from 1 to 10. Modify the program to sum the odd numbers between 5 and 9. Then run the program (you should get a result of 21). | ||
1. The program below is a simple number guessing game. Analyze the program code. Then complete the program. | ||
|
||
```python | ||
### | ||
# Sums numbers from 1 to 10 | ||
# A simple number guessing game | ||
# | ||
x = 1 | ||
sum = 0 | ||
|
||
while x <= 10: | ||
sum += x | ||
x += 1 | ||
|
||
print(...) | ||
``` | ||
|
||
1. The program below is a simple number guessing game. Analyze the program code, then fill in the missing code fragments. | ||
|
||
```python | ||
import random | ||
|
||
# Randomly chosen number to be guessed from 1 to 100 | ||
|
@@ -565,6 +555,43 @@ [email protected] | |
print("Congratulations! You guessed the correct number.") | ||
``` | ||
|
||
1. The following program sums numbers input by the user until they enter 0. Modify the program so that it also calculates the arithmetic mean of the numbers. | ||
|
||
```python | ||
### | ||
# Sums numbers entered by user | ||
# | ||
total_sum = 0 | ||
|
||
while True: | ||
number = int(input("Enter a number (0 to stop): ")) | ||
|
||
if number == 0: | ||
break # Exit the loop when 0 is entered | ||
total_sum += number | ||
|
||
print(f"The total sum of the numbers is: {total_sum}") | ||
``` | ||
|
||
1. The following program calculates the sum of even numbers from 1 to a given number N using a for loop. Modify the program. Replace the 'for' statement with a 'while' statement. | ||
|
||
```python | ||
### | ||
# Calculates the sum of even numbers from 1 to a given number N | ||
# | ||
N = 10 | ||
sum_even = 0 | ||
|
||
# Calculate the sum of even numbers | ||
for i in range(1, N + 1): | ||
if i % 2 == 0: | ||
sum_even += i | ||
|
||
print(f"The sum of even numbers from 1 to {N} is: {sum_even}") | ||
``` | ||
|
||
|
||
|
||
1. The timer.py program takes a number from the user and counts down to zero. Modify the program so that the last five seconds of the counter are displayed in words, i.e. five, four, three, two, one. | ||
|
||
1. The atm.py program simulates a simple ATM where the user can deposit, withdraw, or check the balance. Analyze the program code and then run the program. Next, add two more functions to the program: | ||
|
@@ -577,8 +604,11 @@ [email protected] | |
<https://www.geeksforgeeks.org/python-string-isdigit-method/> | ||
|
||
|
||
|
||
## 6. Practice Makes Perfect | ||
|
||
1. The vowels.py program counts vowels in the text. Modify the program. Replace the 'for' statement with a 'while' statement. | ||
|
||
1. A lamp in a room has three light bulbs. Switch one lights one bulb, and switch two lights the other two bulbs. Write a program that tells you how many bulbs are lit, depending on the state of switch one and switch two. | ||
|
||
```python | ||
|
@@ -756,7 +786,7 @@ [email protected] | |
SURVEY | ||
Are you interested in computer science? (y/n): y\ | ||
Do you like playing computer games? (y/n): n\ | ||
Do you have an Instagram account? (y/n): y\ | ||
Do you have an Instagram account? (y/n): y | ||
|
||
SURVEY RESULTS | ||
Interested in computer science: Yes\ | ||
|
@@ -765,10 +795,10 @@ [email protected] | |
|
||
1. Write a program that converts a decimal number into a binary number. To convert a decimal number to binary, follow these steps: | ||
|
||
1. Read a decimal number from the keyboard.\ | ||
1. Divide the number by 2 and note the remainder.\ | ||
1. Divide the quotient obtained by 2 and note the remainder.\ | ||
1. Repeat the same process till we get 0 as the quotient.\ | ||
1. Read a decimal number from the keyboard. | ||
1. Divide the number by 2 and note the remainder. | ||
1. Divide the quotient obtained by 2 and note the remainder. | ||
1. Repeat the same process till we get 0 as the quotient. | ||
1. Write the values of all the remainders starting from the bottom to the top. That will be the required binary number. | ||
|
||
Sample result: | ||
|
@@ -855,14 +885,6 @@ [email protected] | |
|
||
0 1 1 2 3 5 8 13 21 34 ... | ||
|
||
1. Write a program that calculates the sum and arithmetic mean of numbers entered from the keyboard. Entering 0 ends entering numbers. Sample result: | ||
|
||
Enter number: 15\ | ||
Enter number: 8\ | ||
Enter number: 10\ | ||
Enter number: 0\ | ||
RESULT: Quantity=3, Sum=33, Mean=11 | ||
|
||
1. A natural number greater than 1 is called a prime if it has exactly 2 natural factors with the values 1 and this number. Write a program that finds N leading prime numbers. Read the value of N from the keyboard. Using loop statements check that the number N is divisible only by 1 and by N. | ||
|
||
Prime numbers: 2 3 5 7 11 ... | ||
|
@@ -878,6 +900,4 @@ [email protected] | |
6 13 20 27 34 41 48\ | ||
7 14 21 28 35 42 49 | ||
|
||
1. Write a program that displays 20 integer random numbers in the range of 5 to 10. | ||
|
||
1. Write a program that calculates the sum of integer numbers in the range \<1,5\>. Use the \"while\" statement. | ||
1. Write a program that displays 20 integer random numbers in the range of 5 to 10. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
### | ||
# Counts vowels in the text | ||
# | ||
text = "This is a sample text." | ||
vowels = "aeiouAEIOU" | ||
vowel_count = 0 | ||
|
||
# Count vowels in the text | ||
for char in text: | ||
if char in vowels: | ||
vowel_count += 1 | ||
|
||
print(f"The number of vowels in the text is: {vowel_count}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import time | ||
### | ||
# Counts vowels in the text | ||
# | ||
text = "This is a sample text." | ||
vowels = "aeiouAEIOU" | ||
vowel_count = 0 | ||
|
||
countdown = int(input("Enter the number of seconds to count down: ")) | ||
# Count vowels in the text | ||
for char in text: | ||
if char in vowels: | ||
vowel_count += 1 | ||
|
||
while countdown > 0: | ||
print(countdown) | ||
countdown -= 1 | ||
time.sleep(1) # Wait for 1 second | ||
|
||
print("Time's up!") | ||
print(f"The number of vowels in the text is: {vowel_count}") |