0
import time
import tqdm
import alive_progress
def func():
    def bar(x, y):
        from time import sleep
        from tqdm import tqdm
        for i in tqdm(range(y)):
            sleep(x)
    
    try:
        a = int(input("Pick a number between 1 and 100 for valuable (a): "))
        print("Checking values...")
        bar(0.001, 100)

        if a < 1 or a > 100:
             while (a < 1 or a > 100):
                print("Invalid value, try again")
                a == 0
                a = int(input("Pick a number between 1 and 100 for valuable (a): "))
                print("Checking values...")
                bar(0.001, 100)  
        elif a >= 1 and a <= 100:
                print("Alright, value is in required conditions")

    except ValueError:
        a = ValueError
        while a == ValueError:
            print("Checking values...")
            bar(0.001, 100)  
            print("Invalid value, try again")
            a = int(input("Pick a number between 1 and 100 for valuable (a): "))

I wanted to create a program that checks if user's input is between 1 and 100, also I wanted to check if their input is string so I can turn it to integer. First time when I enter a string it works, but at the second time I got this error:

ValueError: invalid literal for int() with base 10: '/'

I'm newbie in Python, so can do a lot of errors.

I tried a lot of stuff, but nothing worked, so please, help me out!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Sidenote: why have local imports inside a function for modules that are already imported at the module level? – Brian61354270 Aug 08 '23 at 18:46
  • Have you tried using a debugger to step through the code? – Abhijit Sarkar Aug 08 '23 at 18:49
  • 1
    What was your input that returned that error? If it's a fraction it's not a valid numeric. You mgiht be better served using something like ".isnumeric()" as a check rather than `int()`. – scrappedcola Aug 08 '23 at 18:52
  • Your `except ValueError:` is not catching the inner `int(input...)`. I suggest restructuring your code so that all inputs are caught by your `except ValueError:`. See https://stackoverflow.com/q/23294658/2745495 for ideas. – Gino Mempin Aug 08 '23 at 23:05
  • There are many problems with your code but the first one that caught my eye was `int(input(...))`. Resist the urge to compact your code into the fewest lines. Such compaction almost always makes understanding the code more difficult and often obscures bugs. The second issue was the `a = ValueError` assignment inside the `except` block. Since you then do `while a == ValueError:` which is a tautology (always true) and the use of a `while` loop is a red flag that you have a significant problem with the structure of your code. – Kurtis Rader Aug 09 '23 at 03:03
  • Your example doesn't require the `bar` function since it is called with constants and its behavior has nothing to do with the problem you are dealing with. Not to mention the irrelevant `time` and `tqdm` module imports. You also don't show us the input that resulted in the exception. These problems with your question have nothing to do with your inexperience with the Python language. You need to think more about how to debug problems with your code. Such as by eliminating extraneous code. – Kurtis Rader Aug 09 '23 at 03:32

1 Answers1

0

So I changed up some of your code just to make it simpler, but feel free to change anything or ask questions.

As a general rule, try to avoid staying in except statements too long. It's fine to use them for control flow in situations like this but often allowing the loop to return to the start is the best way to go. I also moved the import statements to the top of the file since you don't want to be reimporting them every time. Finally, some of the logic seemed a bit convoluted so I tried to simplify it a bit, although I might have gotten this wrong so feel free to correct me. Part of this is converting a to an int after doing the checking prints, which allows you to delay the except statement running until you need it to.

Full code:

from time import sleep
from tqdm import tqdm


def func():
    def bar(x, y):

        for i in tqdm(range(y)):
            sleep(x)
    while 1:
        try:
            a = input("Pick a number between 1 and 100 for valuable (a): ")
            print("Checking values...")
            bar(0.001, 100)
            a = int(a)
            if a < 1 or a > 100:
                print("Invalid value, try again")
            else:
                print("Alright, value is in required conditions")

        except ValueError:
            print("Invalid value, try again")

func()
Cmd858
  • 761
  • 1
  • 6
  • 10
  • Your answer isn't awful but neither is it great. For example, `while 1` rather than `while True` makes me face palm. The original code doesn't use the value input by the user and neither does your rewrite (other than in the most trivial sense). Your code is less awful than the O.P.'s code but not by much. But that is understandable since the O.P.'s question suggests they don't just have a misunderstanding vis-a-vis Python behavior but will never be a competent programmer. – Kurtis Rader Aug 09 '23 at 03:11