Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter Rate:")
r = float(rate)
if hrs <= 40
pay = hrs * rate
print pay
else hrs > 40
pay = hrs * 15.75
print pay
Here is the error message
486406789.415.py", line 6
if hrs <= 40
^
SyntaxError: invalid syntax
You are missing colons (:) after the conditions. Also, note that else doesn't take a condition, you need to use elif:
if hrs <= 40:
# Here -^
pay = hrs * rate
print pay
elif hrs > 40: # Note the elif
# Here --^
pay = hrs * 15.75
print pay
There are multiple errors in your code. The invalid syntax error is quite easy to fix, just add the colon in the end of the if statement and the else does not take any conditions (elif, however would). This is very basic Python syntax. You can always check the official Python tutorials and documentations as a first step to troubleshoot syntax errors. e.g.:
http://www.tutorialspoint.com/python/python_if_else.htm
after you fix the SyntaxError you will run into other issues when comparing strings with integers, as in hrs <= 40. Instead you want to compare the your converted input h. The same goes for the calculations: use h instead of hrs and r instead of rate.
See if you can solve all the issues. If you don't manage, here is a working example of the code: https://gist.github.com/fabianegli/bae9864e5166fac4dd2baeccd5ed3f8d
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
This is the code. I know that the "question" variables are useless, but that's how i like to do it.
I was supposed to make a pay computation program that asks for input about hours worked and rate of pay and pay 1.5 times the rate for hours worked above 40.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', pay + 'dollars.')
else hrs < 40 :
pay = hrs*rate
print ('Your payment is ', pay + 'dollars.')
I tried to nest it a different way and googled python debugger, but nothing helped. Please tell me how to fix this error and where my mistake is so that i can learn. Thank you !
Hope following code will help you.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
hrs = int(hrs)
rate = int(rate)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', str(pay) + 'dollars.')
elif hrs < 40:
pay = hrs*rate
print ('Your payment is ', str(pay) + 'dollars.')
keep in mind that, input() will always return string datatype, and you have to convert into the int for arithmatic operation,
Wherenver you try to concat string, then you have to convert int to string
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am a total beginner working on an introductory python class but I am getting syntax errors every time I make an if statement. Can anyone help with this?
Here is my code:
def report_size(n: int) -> str:
"""Return 'small' if n is between 0 and 20 inclusive,
'medium' if n is between 21 and 40 inclusive,
and 'large' if n is 41 or greater.
Precondition: n >= 0
>>> report_size(4)
'small'
>>> report_size(24)
'medium'
>>> report_size(45)
'large'
"""
If n <= 20:
return 'small"
elif 20< n <= 40:
return 'medium"
else:
return 'large'
if should not be capitalized.
Also, the apostrophes you use for strings are inconsistent. If you open a string with ' you have to close with ' and if you open with " you should close the string with ".
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My Question is:
Write a function that expects both a person's weekly hours worked (a float value) and their regular hourly wage (a float value). Return a person's total wage for the week (a float value), taking into account overtime.
Any hour worked beyond the 45th hour is considered overtime and is paid at 1.4 times the regular hourly rate. However, if a person works more than 50 hours, they are paid double the hourly rate. Thus, for a maximum of 10 hours of overtime, they will receive 1.4 times the normal rate of pay and thereafter double (2.0 times) the normal rate of pay.
def overtime(working_hours, hourly_rate):
if 50>= working_hours >= 40:
return 40 * hourly_rate + ((working_hours % 40) * 1,4)
elif 40>= working_hours:
return working_hours * hourly_rate
else:
return (40 * hourly_rate) + ((working_hours % 40) * (1,4 * hourly_rate)) + ((working_hours % 50) * (hourly_rate * 2))
TypeError: can't multiply sequence by non-int of type 'float'
Can someone help me? :)
I think you just need to replace , with . in your numbers i.e 1.4 and not 1,4
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to write a program that calculates BMI by asking what one's weight (in kilograms) and height (in meters) is. Why am I getting an invalid syntax error here?
units = input("What units would you like to use? Enter I for Imperial or M for Metric")
weight = input(int("what's your weight?")
height = input(int("what's your height?")
enter image description here
You've picked the wrong order. You should be having int(input()) not the input(int())
And also you have less ) than you should be. Check that for every opening bracket there is a closing one
You have the wrong order of int and input and did forget the closing parenthesis:
units = input("What units would you like to use? Enter I for Imperial or M
for Metric")
weight = int(input("what's your weight?"))
height = int(input("what's your height?"))
Avoid using input() and use raw_input() instead. You also put int() in the wrong place.
Your code should look something like this:
units = raw_input('What units would you like to use?....')
weight = int(raw_input("what's your weight?"))
height = int(raw_input("what's your height?"))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to teach myself Python via Codecademy. However, when I reach this section, it keeps returning the same error, no matter what I do. Anyone else encounter this problem, or know how to fix it? I'm a noob to coding of any way shape or form.
A screenshot of the error can be seen here: https://drive.google.com/file/d/0B7fG5IDRoZ3cXzZxbnlpT3RheHc/edit?usp=sharing
answer = 2
def the_flying_circus():
if ______: answer + 5 = 7
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
else end
The error is:
elif (answer < 5):
^
SynxtaxError: invalid syntax
The error is in your indentation.
You can't have an elif unless it follows an if block. Because your if statement is on one line, and your next print statement is on the next line, you don't meet this requirement. I also don't know what you're intending to do with your if ________: section.
Here's a fix:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
# ...
else: end you are missing a `:`
You have a lot of other syntax errors:
if ______: answer + 5 = 7
You cannot assign to an operator.
Unless end is a variable defined somewhere using else: end is not python syntax
Not sure what you want but from your code this seems more logical:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif answer < 5:
print "As does this!"
else:
return answer