9th day of python challenges 118-123
This commit is contained in:
parent
627802c383
commit
5349baf68b
@ -68,6 +68,10 @@
|
||||
<option value="E501" />
|
||||
<option value="W29" />
|
||||
<option value="E501" />
|
||||
<option value="W29" />
|
||||
<option value="E501" />
|
||||
<option value="W29" />
|
||||
<option value="E501" />
|
||||
</list>
|
||||
</option>
|
||||
</inspection_tool>
|
||||
|
3438
.idea/workspace.xml
3438
.idea/workspace.xml
File diff suppressed because it is too large
Load Diff
12
challenges118-123/challenge-118.py
Normal file
12
challenges118-123/challenge-118.py
Normal file
@ -0,0 +1,12 @@
|
||||
def enter_number():
|
||||
num = int(input('Please enter number : '))
|
||||
return num
|
||||
|
||||
|
||||
def main():
|
||||
num = enter_number()
|
||||
for i in range(num):
|
||||
print(i)
|
||||
|
||||
|
||||
main()
|
39
challenges118-123/challenge-119.py
Normal file
39
challenges118-123/challenge-119.py
Normal file
@ -0,0 +1,39 @@
|
||||
import random
|
||||
|
||||
|
||||
def low_high_range():
|
||||
low = int(input('Enter low number : '))
|
||||
high = int(input('Enter high number : '))
|
||||
num = random.randint(low, high)
|
||||
print(num)
|
||||
return num
|
||||
|
||||
|
||||
def user_ask():
|
||||
ask = int(input('I am thinking of a number... : '))
|
||||
return ask
|
||||
|
||||
|
||||
def check(ask, comp_num):
|
||||
if ask == comp_num:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
comp_num = low_high_range()
|
||||
state = True
|
||||
while state:
|
||||
user_ask_int = user_ask()
|
||||
if check(user_ask_int, comp_num):
|
||||
state = False
|
||||
print('Correct, you win')
|
||||
else:
|
||||
if user_ask_int < comp_num:
|
||||
print('Too low')
|
||||
elif user_ask_int > comp_num:
|
||||
print('Too high')
|
||||
|
||||
|
||||
main()
|
41
challenges118-123/challenge-120.py
Normal file
41
challenges118-123/challenge-120.py
Normal file
@ -0,0 +1,41 @@
|
||||
import random
|
||||
|
||||
|
||||
def addition():
|
||||
question_num_1 = random.randint(5, 20)
|
||||
question_num_2 = random.randint(5, 20)
|
||||
answer = question_num_1 + question_num_2
|
||||
ask_answer = int(input(str(question_num_1) + '+' + str(question_num_2) + '= '))
|
||||
data = (answer, ask_answer)
|
||||
return data
|
||||
|
||||
|
||||
def subtraction():
|
||||
question_num_1 = random.randint(25, 50)
|
||||
question_num_2 = random.randint(1, 25)
|
||||
answer = question_num_1 + question_num_2
|
||||
ask_answer = int(input(str(question_num_1) + '+' + str(question_num_2) + '= '))
|
||||
data = (answer, ask_answer)
|
||||
return data
|
||||
|
||||
|
||||
def check(data):
|
||||
rand_ans = data[0]
|
||||
user_ans = data[1]
|
||||
if rand_ans == user_ans:
|
||||
print('Correct')
|
||||
else:
|
||||
print('Incorrect, the answer is {0}'.format(rand_ans))
|
||||
|
||||
|
||||
def main():
|
||||
print('1) Addition')
|
||||
print('2) Subtraction')
|
||||
operation_ask = int(input('Enter 1 or 2 : '))
|
||||
if operation_ask == 1:
|
||||
check(addition())
|
||||
else:
|
||||
check(subtraction())
|
||||
|
||||
|
||||
main()
|
51
challenges118-123/challenge-121.py
Normal file
51
challenges118-123/challenge-121.py
Normal file
@ -0,0 +1,51 @@
|
||||
def add_name():
|
||||
ask_name = str(input('Enter name to add to names list : '))
|
||||
names.append(ask_name)
|
||||
print_names()
|
||||
|
||||
|
||||
def change_name():
|
||||
print_names()
|
||||
row_ask = int(input('Enter row number to change name : '))
|
||||
ask_name = str(input('Enter name : '))
|
||||
names[row_ask] = ask_name
|
||||
print_names()
|
||||
|
||||
|
||||
def delete_name():
|
||||
print_names()
|
||||
row_ask = int(input('Enter row number to delete name : '))
|
||||
names.pop(row_ask)
|
||||
print_names()
|
||||
|
||||
|
||||
def print_names():
|
||||
count_row = 0
|
||||
for row in names:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
count_row += 1
|
||||
|
||||
|
||||
def main():
|
||||
state = True
|
||||
while state:
|
||||
print('1) Add name')
|
||||
print('2) Change name')
|
||||
print('3) Delete name')
|
||||
print('4) Quit')
|
||||
operation_ask = int(input('Enter action to do : '))
|
||||
if operation_ask == 1:
|
||||
add_name()
|
||||
elif operation_ask == 2:
|
||||
change_name()
|
||||
elif operation_ask == 3:
|
||||
delete_name()
|
||||
elif operation_ask == 4:
|
||||
print('Good bye')
|
||||
state = False
|
||||
else:
|
||||
print('Invalid input, please try again.')
|
||||
|
||||
|
||||
names = ['abdullah', 'ahmed', 'hasanen', 'ali']
|
||||
main()
|
39
challenges118-123/challenge-122.py
Normal file
39
challenges118-123/challenge-122.py
Normal file
@ -0,0 +1,39 @@
|
||||
def add_to_file():
|
||||
name = str(input('Enter name : '))
|
||||
salary = str(input('Enter salary : '))
|
||||
file = open('Salaries.csv', 'a')
|
||||
file.write(str(name + ', ' + salary + '\n'))
|
||||
file.close()
|
||||
|
||||
|
||||
def view_file():
|
||||
file = open('Salaries.csv', 'r')
|
||||
count_row = 0
|
||||
for row in file:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
count_row += 1
|
||||
|
||||
|
||||
def main():
|
||||
state = True
|
||||
count = 0
|
||||
while state:
|
||||
if count != 3:
|
||||
print('1) Add to file')
|
||||
print('2) View all records')
|
||||
print('3) Quit')
|
||||
operation_ask = int(input('Enter the number of your selection : '))
|
||||
if operation_ask == 1:
|
||||
add_to_file()
|
||||
elif operation_ask == 2:
|
||||
view_file()
|
||||
elif operation_ask == 3:
|
||||
print('Good bye')
|
||||
state = False
|
||||
else:
|
||||
print('Invalid input, please try again.')
|
||||
else:
|
||||
state = False
|
||||
|
||||
|
||||
main()
|
65
challenges118-123/challenge-123.py
Normal file
65
challenges118-123/challenge-123.py
Normal file
@ -0,0 +1,65 @@
|
||||
import csv
|
||||
|
||||
|
||||
def add_to_file():
|
||||
name = str(input('Enter name : '))
|
||||
salary = str(input('Enter salary : '))
|
||||
file = open('Salaries.csv', 'a')
|
||||
file.write(str(name + ', ' + salary + '\n'))
|
||||
file.close()
|
||||
|
||||
|
||||
def view_file():
|
||||
file = open('Salaries.csv', 'r')
|
||||
count_row = 0
|
||||
for row in file:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
count_row += 1
|
||||
|
||||
|
||||
def delete_record():
|
||||
file = open('Salaries.csv')
|
||||
file_read = list(csv.reader(file))
|
||||
count_row = 0
|
||||
list_records = []
|
||||
for row in file_read:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
list_records.append(row)
|
||||
count_row += 1
|
||||
file.close()
|
||||
ask = int(input('Enter row number to delete : '))
|
||||
list_records.pop(ask)
|
||||
file_write = open('Salaries.csv', 'w')
|
||||
i = 0
|
||||
for row in range(len(list_records)):
|
||||
file_write.write(list_records[i][0] + ', ' + list_records[i][1] + '\n')
|
||||
i += 1
|
||||
file_write.close()
|
||||
|
||||
|
||||
def main():
|
||||
state = True
|
||||
count = 0
|
||||
while state:
|
||||
if count != 3:
|
||||
print('1) Add to file')
|
||||
print('2) View all records')
|
||||
print('3) Delete a records')
|
||||
print('4) Quit')
|
||||
operation_ask = int(input('Enter the number of your selection : '))
|
||||
if operation_ask == 1:
|
||||
add_to_file()
|
||||
elif operation_ask == 2:
|
||||
view_file()
|
||||
elif operation_ask == 3:
|
||||
delete_record()
|
||||
elif operation_ask == 4:
|
||||
print('Good bye')
|
||||
state = False
|
||||
else:
|
||||
print('Invalid input, please try again.')
|
||||
else:
|
||||
state = False
|
||||
|
||||
|
||||
main()
|
Loading…
Reference in New Issue
Block a user