0

if the user inputed -777 or 77.1 how do i make the program work instead of saying inputed number is a string?

main file

from  helper import *

def main():
#  menu 
    user_selection = ""
    while user_selection != "x":
        print("a - Input something")
        print("x - exit")
        # get the user selection
        user_selection = input("select an option: ")
        print("The user select: "+user_selection)
        # implement the user selection
        if user_selection == "a":
            input_analyst()
            break
       

if __name__ == "__main__":
    main()

helper file

from os.path import exists
from tracemalloc import stop


# this was done purely by hand and google was never used so it might not be size-effective
def input_analyst():

    user_input = input("your input: ")
    # Return True if the string is a numeric string, False otherwise.
    if user_input.isnumeric():
        print("Entered user_input is Integer:", user_input)
        num = int(user_input)  # if its an int set num as the inputed value

    else:
        # if the inputed data is a string print so and stop the function
        print("Entered user_input is a string")
        return

    if user_input.isnumeric():  # if its a int display it's lengh
        print("The int lengh is: ", len(user_input))

    if (num % 2) == 0:  # check if the number is even or odd
        print("{} is an Even number".format(num))
    else:
        print("{} is an Odd number".format(num))

    if num % 7 == 0:  # check if the number is dividable by 7 without a remainder or not and prints how much it would actually be if Divided by 7
        number1 = num
        number2 = 7
        result = number1/number2
        print("{} Dividable by 7 without a remainder".format(num))
        print("{} Dividable by 7 =: {} ".format(num, result))

    else:
        number1 = num
        number2 = 7
        result = number1/number2
        print("{} Isn't dividable by 7 without a remainder".format(num))
        print("{} Divided by 7 =: {} ".format(num, result))

if the user inputed -777 or 77.1 how do i make the program work instead of saying inputed number is a string?

Ilya
  • 41
  • 4
  • You should convert it to number – matebende Aug 30 '22 at 08:28
  • You're checking for divisibility by 2 and 7. Do you want to keep doing that once your program accepts float inputs? It really only makes sense for integers. – Schnitte Aug 30 '22 at 08:31
  • https://stackoverflow.com/questions/8075877/converting-string-to-int-using-try-except-in-python Try to use something like that, to directly convert the input to integer with error handling if it's not possible. – Oivalf Aug 30 '22 at 08:38

2 Answers2

1

you could use a conversion to float together with try / except like:

try:
    num = int(float(user_input))
    print("Entered user input is Integer:", user_input)

except ValueError:
    print("Entered user_input is a string")
    return

In that case you would also accept things like "-77.5" as an integer (-77). If you don't want that you could add a check:

int(float(user_input)) == float(user_input) 

Now one might say that the intermediate conversion to float is not necessary, but from your question it seems like you also want to accept things like 77.0 as intergers.

Nik
  • 1,093
  • 7
  • 26
-1

You would do this at the input() level in what you have called the helper file.

So something like this would work:


user_input = input("your input: ")

# first convert to an int
user_input = int(user_input)

if user_input <0:
    # some logic
    print('warning, negative number, convert to positive')
    user_input = -user_input

print(user_input)

D.L
  • 4,339
  • 5
  • 22
  • 45