Python Forum
Make the answer of input int and str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make the answer of input int and str
#1
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
Reply
#2
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.
Reply
#3
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to make sure an input is from the wanted type astral_travel 16 2,464 Oct-31-2022, 04:11 PM
Last Post: astral_travel
  why don't i get the answer i want CompleteNewb 12 3,381 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,325 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  I am getting the wrong answer, and not sure why riskeay 3 2,042 Nov-05-2020, 08:24 PM
Last Post: deanhystad
  How to make input goto a different line mxl671 2 2,452 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Keeps looping even after correct answer mcesmcsc 2 1,905 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  I'm getting a wrong answer don't know where the bug is 357mag 4 2,802 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 17,356 Feb-14-2018, 09:59 AM
Last Post: wavic
  How to make an input trigger the output once no matter how long input is high cam2363 3 3,249 Feb-05-2018, 01:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020