Oct-30-2022, 06:34 PM
i tried this:
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:
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 ?