Skip to content

Commit 0434a28

Browse files
committed
Adding While Loop In-Depth
1 parent e00aa01 commit 0434a28

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

basics/While Loop/break.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
while True:
2+
print('Enter a number')

basics/While Loop/challenge.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
chances = 3
2+
score = 0
3+
question = {
4+
1: {"question": "10 + 10 = ?", "answer": 20},
5+
2: {"question": "10 * 10 = ?", "answer": 100},
6+
3: {"question": "50 / 2 = ?", "answer": 25},
7+
4: {"question": "20 - 10 = ?", "answer": 10},
8+
5: {"question": "12 + 12 = ?", "answer": 24},
9+
}
10+
11+
index = 1 # Starting index for the questions
12+
while chances > 0 and index <= len(question):
13+
q = question[index] # Access the current question using the index
14+
print(f"Chances: {chances}")
15+
user_answer = int(input(f"{q['question']} "))
16+
if user_answer == q["answer"]:
17+
print("Correct!")
18+
score += 1
19+
else:
20+
print("Wrong!")
21+
chances -= 1
22+
index += 1 # Move to the next question
23+
24+
print(f"Final Score: {score}/{len(question)}")
25+
print("Game Over!")

basics/While Loop/collection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Index 0 1 2 3
2+
studentID = [2000123,2000124,2000125,2000126]
3+
4+
i = 0
5+
6+
while i < len(studentID):
7+
print(f"Student ID: {studentID[i]}")
8+
i += 1
9+
else:
10+
print("End of the student ID list")

basics/While Loop/condtion.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# print("Crush ka ba niya?")
2+
# while True:
3+
# answer = input("Yes or No : ")
4+
# if answer not in ["Yes", 'yes', 'YES', 'y', 'Y']:
5+
# print("Oo nga, d ka niya gusto")
6+
# break
7+
# print('Edi naol')
8+
# break
9+
10+
# Even or Odd
11+
12+
# number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
13+
# i = 0 #Index
14+
# while i < len(number):
15+
# if number[i] % 2 != 0:
16+
# print(f"Odd : {number[i]}")
17+
# else:
18+
# print(f"Even : {number[i]}")
19+
# i += 1
20+
21+
print("Even Or Odd")
22+
while True:
23+
put = input("Enter num : ")
24+
if put.isalpha():
25+
print("Error")
26+
elif put == "":
27+
print("Enter your input")
28+
elif int(put) % 2 == 0:
29+
print(f"Even {put}")
30+
else:
31+
print(f"Odd : {put}")

basics/While Loop/else.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
age = 12
2+
while age < 18:
3+
print(f"You are not an adult yet, you are {age} years old")
4+
age += 1 # every turn it adds 1 to the age
5+
else:
6+
# this will be printed when the while loop is done
7+
print(f"You are now an adult, you are {age} years old")

basics/While Loop/while.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
age = 12
2+
3+
while age < 18:
4+
print(f"You are not an adult yet, you are {age} years old")
5+
age += 1 # every turn it adds 1 to the age

sample.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
age = 17
2+
if not age >= 18:
3+
print("“Too Young”")

0 commit comments

Comments
 (0)