Python Forum
if else with comparison >= does not work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else with comparison >= does not work
#1
hrs = float(input('Enter Hours:'))
rate = float(input ('Enter Rate:'))
h = float(hrs) - 40
r = float (rate) * 1.5
def computepay(h,r):
    retun ((h*r)
if hrs <= 40:
    print (hrs * rate)
else :   
    print(computepay)
Reply
#2
retun ((h*r)

Is that suppose to be return (h*r)

There's also one too many parentheses there.
Reply
#3
does not work HOW? if you get error - post full traceback in error tags.
Robo_Pi is correct that there is extra parenthesis on line 6
Reply
#4
# I got a message: "Bad input on line 7". Can somebody explain
#what is wrong with my "if"? Thanks to all.#

hrs = float(input('Enter Hours:'))
rate = float(input ('Enter Rate:'))
h = float(hrs) - 40
r = float (rate) * 1.5
def computepay():
   return ((h*r + 40*rate)
if hrs <= 40:
    print (hrs*rate)
else:   
     print("Pay", computepay)
Reply
#5
I copied and pasted your code and ran it with python3 on Windows 10, I get the following output:

Output:
Enter Hours:43 Enter Rate:23 Pay <function computepay at 0x0581C150>
Running with python2, I get:
Output:
Enter Hours:43 Enter Rate:23 ('Pay', <function computepay at 0x04F05930>)
So from where I am, the only problem is that you're not invoking the computepay function.

Are you typing this in an interpreter or putting it in a file and running it?
Reply
#6
Robo_Pi already pointed it out.
return ((h*r + 40*rate)
You have an extra parentheses. (

Line 10, the error is because you referenced the function instead of calling it, because you had no parentheses.
From this
print("Pay", computepay)
to this
print("Pay", computepay())
Try it now
hrs = float(input('Enter Hours:'))
rate = float(input ('Enter Rate:'))
h = float(hrs) - 40
r = float (rate) * 1.5
def computepay():
  return (h*r + 40*rate)
if hrs <= 40:
  print (hrs*rate)
else:   
  print("Pay", computepay())
Reply
#7
My auto-grader says, "Bad input on line 7" that is when if statement starts. Please tell me, why?
Reply
#8
On line 6, you have an open parenthesis.
On line 7, python is expecting you to close that parenthesis. Because you don't, whatever you put there is bad input.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020