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

Hey guys! I am a beginner learning python and my brain is the size of a walnut can yall please tell me what is wrong with this code?

what is wrong with this code? it crashes and i dont know why:

#this is for like converting days to hours
Question = input('Hey you, tell me a number of days and I will convert it into hours : ')
if Question.isdigit():
# if the user inputs a 0 which is not a valid positive number it prints this "This is a zero, please enter a positive number"
    if Question == 0:
        print("This is a zero, please enter a positive number")
#if the user inputs a negative number it prints this: "This is a negative number please enter a positive number"
    if Question < 0:
        print("This is a negative number please enter a positive number")
# now this one down over here is when the user inputs a valid positive number
    if Question > 0:
        print(f'{Question} Days converted to hours = {Question*24}')
# this one here is if the user inputs a letter
    else:
        print('You have entered a letter, please enter a positive number')```

>Solution :

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

You are not converting your string input to an integer. You can do this using :

Question = int(input(...))

Also, to prevent conversion errors, you can double-check (if you want) that it is in fact an integer by using:

Question = input(...)
if Question.isdigit():
    Question = int(Question)
    # etc.
else:
    print("Input was a letter, not a number")
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