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
#8
i tried this:

import random

RNumber = random.randint(1,10)

try:
    user_input = int(input("please guess a number between 0 and 10: \n"))
except ValueError:
    print("you have typed an invalid input")

count = 1

while user_input != RNumber:
    try:
        if user_input > RNumber:
            user_input = int(input("choose a lower number: \n"))
        elif user_input < RNumber:
            user_input = int(input("choose a higher number: \n"))
    except ValueError:
        print("you have typed an invalid input")
    count += 1

print(f"the number you chose is correct {RNumber}\n")

print(f"you have guessed the right number in {count} times")
it works when in the first time i am asked to enter a number, and then after i choose some number - i type some string, it works okay,
but if right on the first time i'm prompted i enter a string - it gives the right output - but then also an error...
the output is this:
Output:
/home/tal/PycharmProjects/pythonProject1/venv/bin/python /home/tal/code/pycharm-community-2020.2.3/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 42581 --file /home/tal/PycharmProjects/pythonProject2/experiment.py please guess a number between 0 and 10: you have typed an invalid input Traceback (most recent call last): File "/home/tal/code/pycharm-community-2020.2.3/plugins/python-ce/helpers/pydev/pydevd.py", line 1496, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/home/tal/code/pycharm-community-2020.2.3/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/tal/PycharmProjects/pythonProject2/experiment.py", line 12, in <module> while user_input != RNumber: NameError: name 'user_input' is not defined Process finished with exit code 1
how come this is so ? is there a way to correct it ?
Reply


Messages In This Thread
RE: how to make sure an input is from the wanted type - by astral_travel - Oct-30-2022, 06:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,326 Nov-24-2024, 12:58 PM
Last Post: deanhystad
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 4,895 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Make the answer of input int and str enderfran2006 2 2,863 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  How to make input goto a different line mxl671 2 3,424 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Type hinting - return type based on parameter micseydel 2 3,269 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 3,730 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  catch input type error mcmxl22 5 4,350 Aug-11-2019, 07:33 AM
Last Post: wavic
  Getting type from input() function in Python 3.0 leodavinci1990 7 5,323 Jul-29-2019, 08:28 PM
Last Post: avorane
  how i can check the input type? Firdaos 3 3,771 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 4,159 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