Python Forum

Full Version: Make the answer of input int and str
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I am trying to figure out how to make the answer of an input be both a string and an integer.
Here is my code:
event = int(input("When is your next event? or press 'Enter' to quit or type FIND to find a date"))
I am trying to make a calender
the event will be a number for the day e.g. 16 or 31 and the 'FIND' is when you want to find a previous event you have inputed
I know that int( means integer but apart from that I am stuck
Python variables aren't both strings and integers.  input() returns a string.  So what you could do is:
  • read the input into a variable (it will be a string variable)
  • See if it is FIND
  • If it isn't FIND, try to assign the int() value to a new variable
Right now you're trying to convert the input into an int, and if it's FIND, that conversion will fail.
Compare with "FIND" before you cast the str into an int.

event = input("When is your next event? or press 'Enter' to quit or type FIND to find a date")
if event.strip().upper() == "FIND":
    print("Do find...")
else:
    try:
        value = int(event)
    except ValueError:
        print(f"Invalid user input {event} is not a valid integer")
    print("User entered value:", value)