How do i make the program exit by entering 0 - python-3.x

# ABC Inc., Gross Pay Calculator!
# Enter employee's name or 0 to quit : Nathan
# Enter hours worked : 35
# Enter employee's pay rate : 10.00
# Employee Name : Nathan
# Gross Pay: 350.0
# Enter next employee's name or 0 to quit : Toby
# Enter hours worked : 45
# Enter employee's pay rate : 10
# Employee Name : Toby
# Gross Pay : 475.0
# (overtime pay : 75.0 )
# Enter next employee's name or 0 to quit : 0
# Exiting program...
How do i make the input for 0 print "exiting program" then exit?
print('ABC inc., Gross Pay Calculator!')
name = input("Enter employee's name or 0 to quit:")
hours = float(input("Enter hours worked:"))
payrate = float(input("Enter employee's pay rate:"))
print("Employee Name:", name)
grosspay = hours * payrate
print("Gross pay:", grosspay)
if hours > 40:
print("(Overtime pay:", (hours - 40) * payrate * 1.5)
while name!=0:
name = input("Enter next employee's name or 0 to quit:")
hours = float(input("Enter hours worked:"))
payrate = float(input("Enter employee's pay rate:"))
print("Employee Name:", name)
grosspay = hours * payrate
print("Gross pay:", grosspay)
if hours > 40:
print("(Overtime pay:", (hours - 40) * payrate*1.5)
else:
print ('Exiting program...')

Do not repeat code like that. Instead use while True: and conditional break as the appropriate place. This is the standard 'loop-and-a-half' idiom in Python.
print('ABC inc., Gross Pay Calculator!')
while True:
name = input("Enter employee's name or 0 to quit:")
if name == '0':
print ('Exiting program...')
break
hours = float(input("Enter hours worked:"))
payrate = float(input("Enter employee's pay rate:"))
print("Employee Name:", name)
grosspay = hours * payrate
print("Gross pay:", grosspay)
if hours > 40:
print("(Overtime pay:", (hours - 40) * payrate * 1.5)
I would replace '0' in the prompt with 'nothing' and make the test if not name:, but this is a minor issue.

Related

Why do I get duplicate output when using a for loop over a list?

Hello I have problem looping when printing data in a list.
def inputScore(scor) :
for i in range(len(scor)) :
scor[i] = int(input("Enter Score Number : "))
def Display(scr) :
for i in scr:
if i >= 86 and i <= 100 :
x = "A"
elif i >= 70 and i < 86 :
x = "B"
elif i >= 60 and i < 70 :
x = "C"
elif i >= 50 and i < 60 :
x = "D"
elif i >= 0 and i < 50 :
x = "E"
else :
print("Invalid Score")
for i in range(0,3) : # If I remove this code "for..", it will error "IndexError: list index out of range"
print("Score Number",scr[i],"Letter Grades", x)
def main () :
scor = [int]*3
inputScore(scor)
Display(scor)
main()
Example:
# Input :
85
60
40
# Output that I want :
Score Number 85 Letter Grades A
Score Number 60 Letter Grades C
Score Number 40 Letter Grades E
# Output that I got :
Score Number 85 Letter Grades A
Score Number 60 Letter Grades A
Score Number 40 Letter Grades A
Score Number 85 Letter Grades C
Score Number 60 Letter Grades C
Score Number 40 Letter Grades C
Score Number 85 Letter Grades E
Score Number 60 Letter Grades E
Score Number 40 Letter Grades E
There are 3 looping for Letter Grades (A = 3 times, C = 3 times and E = 3 times), I tried to give for i in range(0,3) for stop looping but it doesn't work, Letter Grades always prints 9 times with 3 A, 3 C, and 3 E. How to solve it and make it output like in the example above?
The inner for loop is unnecessary. The outer for loop already iterates through the scores. In each iteration, i is a score, not an index -- to get an index, you use range. Therefore, there's no need to index the scr list at all -- you would print i itself rather than scr[i].
Also, with the current code, the grade for 85 would be B rather than A. Perhaps you need to adjust the bounds for the A and B grades.
Another issue is that for an invalid score, it would still attempt the final print. This would fail if an invalid score occurred as the first score (since x wouldn't be defined). If an invalid score occurred as a subsequent score, the print would show you the grade for the previous score, which you don't want. You can get around this by setting x to be the empty string at the start of each iteration and checking if it has a non-empty value before doing the final print of the grade.
The following code resolves the issues discussed:
def inputScore(scor):
for i in range(len(scor)):
scor[i] = int(input("Enter Score Number: "))
def display(scr):
for i in scr:
x = ""
if i >= 85 and i <= 100:
x = "A"
elif i >= 70 and i < 85:
x = "B"
elif i >= 60 and i < 70:
x = "C"
elif i >= 50 and i < 60:
x = "D"
elif i >= 0 and i < 50:
x = "E"
else:
print("Invalid Score")
if x:
print("Score Number:", i, "Letter Grade:", x)
def main():
scor = [int] * 3
inputScore(scor)
display(scor)
main()

Print list1 with list2 python

I dont know how to search this code in internet so I ask here
My code :
# This code is in Tes.py
n = [str]*3
x = [int]*3
MyLib.name(n)
MyLib.number(x)
MyLib.Result(n,x)
# This code in MyLib.py with 3 def
def name(data) :
for i in range (3) :
n[i] = str(input("Enter Name : ")
def number(data) :
for s in range (3) :
x[i] = int(input("Enter Number : ")
def result(data1,data2) :
for i in data1 :
for i in data2 :
print("Your Name",n,"Your Number",x)
examples :
input 1 : Jack
Rino
Gust
input 2 : 1232
1541
2021
output what I want : Your Name Jack Your Number 1232
Your Name Rino Your Number 1541
Your Name Gust Your Number 2021
output that i got : Your Name Jack Your Number 1232
Your Name Jack Your Number 1541
Your Name Jack Your Number 2021
Your Name Rino Your Number 1232
Your Name Rino Your Number 1541
Your Name Rino Your Number 2021
Your Name Gust Your Number 1232
Your Name Gust Your Number 1541
Your Name Gust Your Number 2021
How to get output like what I want, I want to search it in the google but I dont know what I must type.
Is this what you mean?
for i in range(min(len(n), len(x))):
print("Your Name",n[i],"Your Number",x[i])
total = 3
n = [str]*total
x = [int]*total
for i in range (total):
n[i] = str(input("Enter Name : "))
for i in range (total):
x[i] = int(input("Enter Number : "))
for i in range (total):
print("Your Name",n[i],"Your Number",x[i])
If you give this code the input you mentioned, it will show the desired result. As you wrote two loops,
for i in n :
for i in x :
your print will be triggered 3*3 = 9 times! You just need one single loop, since at the both input you are taking same number of inputs (3 names and 3 numbers!). Even I would say why do you need 3 loops? why not just this:
total = 3
n = [str]*total
x = [int]*total
for i in range (total):
n[i] = str(input("Enter Name : "))
x[i] = int(input("Enter Number : "))
print("Your Name",n[i],"Your Number",x[i])

its a exercise with a solution plz explain me a if statement in this

hours = int(input("Enter the hours: "))
rate = float(input("Enter the rate: "))
if hours > 40:
pay = (hours - 40) * rate * 1.5 + 40 * rate
else:
pay = hours * rate
print("pay: ", pay)
If hours greater than 40
if hours > 40:
pay = (hours - 40) * rate * 1.5 + 40 * rate
If hours less than or equal to 40
else:
pay = hours * rate

Simple python program using while loops, off by one error

I have tried to figure out where the off by one error is and have had no luck. I am an absolute beginner at programming. The increase is supposed to start on year two, but my code adds it to year one. Thanks in advance for any and all help!
##
# salaryschedule.py
# 2/15/2017
# This program will calculate and print the salary schedule for years 1
# through 30 for the teachers in Murdock County. For each year of
# experience, up to 20 years, the salary is increased by 2%.
# Each year after 20, the salary stays the same as year 20.
##
RATE = 2.0
INITIAL_SALARY = 37238.00
salary = INITIAL_SALARY
year = 1
print("Murdock County")
print("Teacher Salary Schedule")
print()
print("Year Salary")
print("---- ------")
while year < 31 :
increase = salary * RATE / 100
salary = salary + increase
print("%4d %15.2f" % (year, salary))
year = year + 1
You only have to print the salary before increasing it.
RATE = 2.0
INITIAL_SALARY = 37238.00
salary = INITIAL_SALARY
year = 1
print("Murdock County")
print("Teacher Salary Schedule")
print()
print("Year Salary")
print("---- ------")
while year < 31 :
print("%4d %15.2f" % (year, salary))
increase = salary * RATE / 100
salary = salary + increase
year = year + 1
Output:
Murdock County
Teacher Salary Schedule
Year Salary
---- ------
1 37238.00
2 37982.76
3 38742.42
4 39517.26
5 40307.61
6 41113.76
7 41936.04
8 42774.76
9 43630.25
10 44502.86
11 45392.91
12 46300.77
13 47226.79
14 48171.32
15 49134.75
16 50117.45
17 51119.79
18 52142.19
19 53185.03
20 54248.73
21 55333.71
22 56440.38
23 57569.19
24 58720.57
25 59894.99
26 61092.89
27 62314.74
28 63561.04
29 64832.26
30 66128.90
Your while loop calculates the increase for the year, which is one, and then prints that. But you want to simply print year one as is, correct? So, the simple solution is just moving the print setting to the top of the loop. Year one will be calculated correctly, and then it will change the numbers of the salary and increase before restarting the loop. Like this:
while year < 31 :
print("%4d %15.2f" % (year, salary))
increase = salary * RATE / 100
salary = salary + increase
year = year + 1
Take note, that it will calculate the next salary/increase on the last loop, but not print it. Alternatively, add a print line before the loop that prints year one, such that the loop starts on year 2 (full code for second example):
RATE = 2.0
INITIAL_SALARY = 37238.00
salary = INITIAL_SALARY
year = 1
print("Murdock County")
print("Teacher Salary Schedule")
print()
print("Year Salary")
print("---- ------")
#Changed to so that salary does not increase after 20 years.
print("%4d %15.2f" % (year, salary))
while year < 31 :
if year < 20:
increase = salary * RATE / 100
salary = salary + increase
year = year + 1
print("%4d %15.2f" % (year, salary))
else:
year = year + 1
print("%4d %15.2f" % (year, salary))
Gives the output below, note that salary does is increased on year 20. If you do not want this, change the 20 in the if statement, to 19, so that it stops adding the increase one year earlier:
Murdock County
Teacher Salary Schedule
Year Salary
---- ------
1 37238.00
2 37982.76
3 38742.42
4 39517.26
5 40307.61
6 41113.76
7 41936.04
8 42774.76
9 43630.25
10 44502.86
11 45392.91
12 46300.77
13 47226.79
14 48171.32
15 49134.75
16 50117.45
17 51119.79
18 52142.19
19 53185.03
20 54248.73
21 54248.73
22 54248.73
23 54248.73
24 54248.73
25 54248.73
26 54248.73
27 54248.73
28 54248.73
29 54248.73
30 54248.73
31 54248.73

Why doesn't my code work? Please look and see if you can help me

Assuming that the population of a country A is of the order of 80 000 inhabitants with a rate
annual growth of 3% and the population B is the population of 20 0000 inhabitants at a rate of growthprogram to calculate and write the number of years required
for the population of country A exceeds or equals the population of country B, kept rates
growth.
After do this I need to do another program for to change the previous program allowing the user to inform the population and growth rates initials. Validate input and allow repeat operation.
I did this form but doesn't work ...
here
years = int(input("anecesary years: "))
populA = 80000
populB = 200000
years = 0
growthA = 0.03
growthB = 0.015
while populA > populB:
years += 1
populA = populA + (populA * growthA)
populB = populB + (populB * growthB)
print("after %i years the country A exceeded the country B :",years)
print("P A: ", populA)
print("P B: " ,populB)
populA = 80000
populB = 200000
while populA > populB:
There is your problem, your code will not run as populA is less than populB starting off not greater than >.
Change to:
while populA < populB:
You also reset years to 0 after your years = int(input("anecesary years: ")) when you use years = 0 which I doubt is what you want.
So your code should look something like the following, remove years = 0 and make sure populA = populA + (populA * growthA) etc.. is inside the while loop:
years = int(input("anecesary years: "))
populA = 80000
populB = 200000
growthA = 0.03
growthB = 0.015
while populA < populB:
years += 1
populA += populA * growthA # same as populA = populA + (populA * growthA)
populB += populB * growthB
print("after %i years the country A exceeded the country B :", years)
print("P A: ", populA)
print("P B: ", populB)

Resources