0

What is wrong with this code? It crashes and I don't 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')

2 Answers2

1

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")
Larry the Llama
  • 958
  • 3
  • 13
  • still doesn't work : Unresolved attribute reference 'isdigit' for class 'int' – user17407796 Nov 14 '21 at 05:23
  • You need to do that before you convert to an integer, because it only works for string. – Larry the Llama Nov 14 '21 at 05:23
  • is it like this? – user17407796 Nov 14 '21 at 05:28
  • Question = int(input('Hey you, tell me a number of days and i will convert it into hours : ')) if Question.isdigit(): Question = int(Question) if Question == 0: print("This is a zero, please enter a positive number") if Question < 0: print("This is a negative number please enter a positive number") if Question > 0: print(f'{Question} Days converted to hours = {Question*24}') else: print('You have entered a letter, please enter a positive number') – user17407796 Nov 14 '21 at 05:29
  • wait i was about to say it wasnt working but then i realized it wasnt working because i already assigned what "Question" will be so then i removed the "int" from it, but wait whenever i add a string it does not print out "You have entered a letter, please enter a positive number" – user17407796 Nov 14 '21 at 05:39
  • wait a second, i think i figured it out! i was assigning "else:" to "Question = int(Question)" now it works perfectly fine! thanks! – user17407796 Nov 14 '21 at 05:47
0

It's good to use isdigit() method if you want to allow integers only. But, it's always better to use try-exceptstatements than isdigit() method specifically for negative numbers since they contain a symbol (-).
So, it needs to be like this

#this is for like converting days to hours
try:
    Question = int(input('Hey you, tell me a number of days and I will convert it into hours : '))
# 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
except:
        print('You have entered a letter, please enter a positive number')
Python learner
  • 1,159
  • 1
  • 8
  • 20