-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice_assignment_python_session_3.py
More file actions
158 lines (127 loc) · 4.07 KB
/
practice_assignment_python_session_3.py
File metadata and controls
158 lines (127 loc) · 4.07 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
"""Practice_Assignment : Python - Session 3.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1SSgBOtQeaVSRKfOBfDjgil6_u7rmOpJH
## Part 1: Conditional Statements - Think Like a Coder
✅ 1. Postive, Negative, or Zero
"""
# This is a helper function which us useful for all the number input validation on below code snippets
def valid_num_input(input_num):
return input_num and all([char in [str(d) for d in range(10)] for char in input_num])
input_num = 0
# Asking user to enter the input and parsing it to number
while True:
# Removing white spaces from the input by using .strip()
input_num = input("Enter a number: ").strip()
# The problem with negatives is parsing it with int() or float() directly will lead to
# code-break, we can handle this with try/expect but I tried to make sure I don't use them
# checking if string is starting with any sign + or - and seperating sign with next character
if input_num[:1] in ('+', '-'):
unsigned_num = input_num[1:]
else:
unsigned_num = input_num
# if each charater entered belong to no other than 0 to 9 digits and non empty input
is_valid_input = valid_num_input(unsigned_num)
if is_valid_input:
input_num = float(input_num)
break
else:
print("Invalid input. Please enter a valid number.")
# Checking for positive, negative or zero
if input_num > 0:
print("Positive.")
elif input_num < 0:
print("Negative.")
else:
print("Zero.")
"""✅ 2. The Number Game"""
user_input = 0
while True:
user_input = input("Enter a number: ").strip()
is_valid_input = valid_num_input(user_input)
if is_valid_input:
user_input = int(user_input)
break
else:
print("Invalid input. Please enter a valid number.")
if user_input % 3 and user_input % 5 == 0:
print("FizzBuzz")
elif user_input % 3 == 0:
print("Fizz")
elif user_input % 5 == 0:
print("Buzz")
else:
print(user_input)
"""✅ 3. Movie Tciket Price checker"""
user_age = 0
while True:
user_age = input("Enter your age: ").strip()
is_valid_input = valid_num_input(user_age)
if is_valid_input and user_age.isdigit() and int(user_age) < 100:
user_age = int(user_age)
break
else:
print("Invalid input. Age can only be an integer number and less than 100.")
price = 0
if user_age > 60:
price = 120
elif 13 <= user_age <= 60:
price = 150
elif 5 <= user_age <= 12:
price = 100
else:
pass
print(f"Your ticket price is {'Free' if price == 0 else f'₹{price}'}.")
print('Thank you for visiting!')
"""## 🔁 Part 2: For Loops – Automate the Fun!
✅ 4. Multiplication Table Maker
"""
input_mul = 0
while True:
input_mul = input('Enter any integer number: ').strip()
is_valid_input = valid_num_input(input_mul)
if is_valid_input and input_mul.isdigit():
input_mul = int(input_mul)
break
else:
print("Invalid input. Please enter only integers.")
for i in range(1, 11):
print(f'{input_mul} x {i} = {input_mul * i}')
"""✅ 5. Print the Stars ⭐"""
input_stars = 0
while True:
input_stars = input('Enter any integer number: ').strip()
is_valid_input = valid_num_input(input_stars)
if is_valid_input and input_stars.isdigit():
input_stars = int(input_stars)
break
else:
print("Invalid input. Please enter only integers.")
for i in range(1, input_stars + 1):
print('*' * i)
"""✅ 6. Add It Up!"""
print(sum(range(1, 101)))
import time
while True:
input_timer_len = input('Enter your time length in seconds: ').strip()
is_valid_input = valid_num_input(input_timer_len)
if is_valid_input and input_timer_len.isdigit():
input_timer_len = int(input_timer_len)
break
else:
print("Invalid input. Please enter only integers.")
while True:
print("Liftoff! 🚀 " if input_timer_len == 0 else input_timer_len)
input_timer_len -= 1
time.sleep(1)
if input_timer_len == -1:
break
"""✅ Secret Code Detector"""
import getpass
pass_key = 'python123'
password = getpass.getpass('Enter your password: ')
if password == pass_key:
print("Access Granted ✅")
else:
print('Wrong password! Try again 🔐')