Python Forum
how to make sure an input is from the wanted type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to make sure an input is from the wanted type
#11
It is the input on line 8 that is displaying the str that prompt points to.
Reply
#12
i think i have got it, thank you !
Reply
#13
(Oct-30-2022, 07:46 PM)astral_travel Wrote: okay thank you Dean,

i have a question for you, - i noticed that on lines 14 and 16 you used prompt = "Choose a lower number: " (and "higher" in line 16 accordingly) - how can one just put a variable and have its value printed out ? don't you have to use the print() method for that ?
That is not the kind of questions you should be asking. You should figure that out by yourself. I assume you are using some sort of IDE that allows single stepping through programs. If not, download one immediately! Using the debugger in your IDE, single step through the program. When is the message printed? Was it when the variable is assigned or somewhere else?

You will learn much faster if you poke around inside programs to learn how they work.
ndc85430 likes this post
Reply
#14
Over two years ago, you got a reply from ndc85430, that covered this exact topic (https://python-forum.io/thread-25302.html)

Maybe you should go back and check what you've posted and what answers you got, that way you can learn from what others have taken the time and trouble to post.

You can also learn a good deal of the basics (as well as more advanced topics) from the likes of https://www.w3schools.com/python/, among others.
wavic likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#15
rob101 - i think that in the thread you directed me to - the question was different, and the code there is obviously much more simpler and less well written...

deanhystad - thank you for the answer, i have PyCharm Community, but i don't know exactly what to do - do i simply put on the red dot on the left side and run the debug, i really haven't tried before what you suggest

i really am not trying to get easy answers, sometimes i just don't know, but i look up in different websites to find answers, it's only when i haven't found an answer elsewhere is when i ask here for clarifications...
Reply
#16
If you validate the user input, you don't need to raise an exception.

Some input is easy to validate; this is one such case: a integer number between 1 and 10.

def check_input(n):
    """check the user input for a valid number.
    if valid, return the number, otherwise return 'None'"""
    for number in n:
        if number.isnumeric():
            pass
        else:
            return None
    return int(n)


user_input = ""
while not user_input:
    user_input = check_input(input("Pick a number between 1 to 10: "))
    if user_input not in range(1, 11):
        print("Out of range.")
        user_input = ""
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#17
ok rob101 thank you, this helps me much, thank you !
rob101 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,356 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Make the answer of input int and str enderfran2006 2 2,017 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  How to make input goto a different line mxl671 2 2,472 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Type hinting - return type based on parameter micseydel 2 2,514 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,770 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  catch input type error mcmxl22 5 3,078 Aug-11-2019, 07:33 AM
Last Post: wavic
  Getting type from input() function in Python 3.0 leodavinci1990 7 3,785 Jul-29-2019, 08:28 PM
Last Post: avorane
  how i can check the input type? Firdaos 3 2,871 Dec-13-2018, 11:39 PM
Last Post: wavic
  How to make an input trigger the output once no matter how long input is high cam2363 3 3,266 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