-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile_loop.py
More file actions
43 lines (36 loc) · 872 Bytes
/
Copy pathwhile_loop.py
File metadata and controls
43 lines (36 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# int i = 0;
# while (i < 5) {
# cout << i << endl;
# i++;
# } its in c++
# in python
i = 0
while i < 5:
print(i)
i += 1 # No i++ in Python!
# Basic countdown
count = 5
while count > 0:
print(count)
count -= 1
print("Blast off!")
# User input until valid
user_input = ""
while user_input != "quit":
user_input = input("Enter 'quit' to exit: ")
print("You entered: ,user_input")
# f string is also used to format the string in python and
# it is more efficient than + operator to concatenate the string and variable.
# Infinite loop with break
while True:
command = input("Enter command: ")
if command == "exit":
break
print(f"Executing: {command}")
# Game loop
health = 100
while health > 0:
damage = int(input("Enter damage: "))
health -= damage
print(f"Health: {health}")
print("Game Over!")