0

I'm new to Python. I'm thinking about this error for 2 days now. I feel the answer is simple but it eludes me still. My code is for translating time periods into seconds:

#input
input_time_from_user = input('Enter time in years, months, weeks, days, hours:')
input_time = input_time_from_user.split()[0]
input_category = input_time_from_user.split()[1]
ConvertedInteger_input_time = int(input_time)

#processing
if input_category == 'years':
     time_in_seconds = ConvertedInteger_input_time*365*24*60*60
elif input_category == 'months':
     time_in_seconds = ConvertedInteger_input_time*30.42*24*60*60
elif input_category == 'days':
     time_in_seconds = ConvertedInteger_input_time*24*60*60
else input_category == 'hours':
     time_in_seconds = ConvertedInteger_input_time*60

print('The converted time of {} in seconds is {}'.format(input_time_from_user, time_in_seconds))

In this current form, I get an error statement:

File "<ipython-input-21-91bf6c885d46>", line 16
else input_category == 'hours':
                   ^
    SyntaxError: invalid syntax

If I disable the else statement, it runs ok. What could've gone wrong ? Thanks !

2 Answers 2

1

Your last statement is an else, but you provided a condition, it should be an elif

The else statement is to use without any condition, it basically means "everything else".

For reference: https://www.w3schools.com/python/python_conditions.asp

if input_category == 'years':
     time_in_seconds = ConvertedInteger_input_time*365*24*60*60
elif input_category == 'months':
     time_in_seconds = ConvertedInteger_input_time*30.42*24*60*60
elif input_category == 'days':
     time_in_seconds = ConvertedInteger_input_time*24*60*60
elif input_category == 'hours':
     time_in_seconds = ConvertedInteger_input_time*60
Sign up to request clarification or add additional context in comments.

Comments

0

According to the Python documentation

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print('Negative changed to zero')
... elif x == 0:
...     print('Zero')
... elif x == 1:
...     print('Single')
... else:
...     print('More')
...

An else statement does not take a logical test.

To fix your code:

#input
input_time_from_user = input('Enter time in years, months, weeks, days, hours:')
input_time = input_time_from_user.split()[0]
input_category = input_time_from_user.split()[1]
ConvertedInteger_input_time = int(input_time)

#processing
if input_category == 'years':
     time_in_seconds = ConvertedInteger_input_time*365*24*60*60
elif input_category == 'months':
     time_in_seconds = ConvertedInteger_input_time*30.42*24*60*60
elif input_category == 'days':
     time_in_seconds = ConvertedInteger_input_time*24*60*60
elif input_category == 'hours':
     time_in_seconds = ConvertedInteger_input_time*60
else:
     pass
     #put a fallthrough condition here, if needed. 

print('The converted time of {} in seconds is {}'.format(input_time_from_user, time_in_seconds))

If you need an else condition, put that at the end where I have pass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.