Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If user adds "m" or "h" to input, print "mins" or "hours"

I’m wanting to print "mins" if the user inputs a number with "m" after; and "hours" if user inputs number with "h" after.

My code:

import pyautogui
xtime = (input("Enter time (add: m for mins, or h for hrs): "))

if xtime + "m":
    print("mins")
elif xtime + "h":
    print("hours")

I don’t know how to do this, I’m just guessing, could someone please help me? Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem of your code is that what you’re doing is adding a "m" if the string xtime is not null.

You should use str.endswith Python built-in function:

xtime = (input("Enter time (add: m for mins, or h for hrs): "))

if xtime.endswith('m'):
    print("mins")
elif xtime.endswith('h'):
    print("hours")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading