Python Forum
how to make sure an input is from the wanted type - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to make sure an input is from the wanted type (/thread-38535.html)

Pages: 1 2


RE: how to make sure an input is from the wanted type - Yoriz - Oct-30-2022

It is the input on line 8 that is displaying the str that prompt points to.


RE: how to make sure an input is from the wanted type - astral_travel - Oct-30-2022

i think i have got it, thank you !


RE: how to make sure an input is from the wanted type - deanhystad - Oct-31-2022

(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.


RE: how to make sure an input is from the wanted type - rob101 - Oct-31-2022

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.


RE: how to make sure an input is from the wanted type - astral_travel - Oct-31-2022

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...


RE: how to make sure an input is from the wanted type - rob101 - Oct-31-2022

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 = ""



RE: how to make sure an input is from the wanted type - astral_travel - Oct-31-2022

ok rob101 thank you, this helps me much, thank you !