-1

I’m quite new to coding and I’m having a tough time setting my code to only accept positive values. When negative values and/or 0 are used I want to output a statement like “Try again.” It has to be a while statement so I tried a couple different options and they all failed!

def get_side(a,b,c):

    a = int(input('Enter Side Length #1: '))


      while a < 0:

          a = int(input('Try Again! Enter Side Length #1: '))
        
    b = int(input('Enter Side Length #2: '))


    c = int(input('Enter Side Length #3: '))

get_side(‘a’,’b’,’c’)

This is what I have so far, I got it to ask me what each side was but it doesn’t do that any longer.

buran
  • 13,682
  • 10
  • 36
  • 61
Sariti
  • 19
  • 1
  • Does this answer your question? [How do I get python to only accept positive integer as raw\_input?](https://stackoverflow.com/questions/29597666/how-do-i-get-python-to-only-accept-positive-integer-as-raw-input) – Karan Owalekar Feb 20 '22 at 06:29
  • @Sariti what is the problem? It looks great, if a is a negative number it will ask you to re-enter it. – Kovy Jacob Feb 20 '22 at 06:30
  • Why are you taking a, b and c as parameters to the function then overwriting all the values? – Erik McKelvey Feb 20 '22 at 06:31
  • 1
    Those funky quotation marks on the last line are definitely going to cause problems. If you're writing this code in something like Word, then definitely stop doing that now and use a real code editor. Smart quotes are the least of your problems. – Silvio Mayolo Feb 20 '22 at 06:35

1 Answers1

4

There's a bunch of problems with your code:

get_side(‘a’,’b’,’c’)

Calling a function like that will work (assuming your editor changes it to get_side('a','b','c') with regular quotes, as @silviomayolo correctly points out, you should use a proper text or code editor), since your function's signature is:

def get_side(a,b,c):

But it appears you really just want a, b, and c to be variables with the three sizes that have been entered. What your call does is assigning a the value 'a', b the value 'b', etc. You don't need those values in your function, so there's no point.

Also, your function isn't returning anything and what you're doing won't make the values available outside the function. You need a return statement (or their evil cousin, a global variable).

Your function also repeats itself three times, a more efficient solution would be to just have one function that requests a single value, and call it three times.

Finally, you also don't want 0 as a value, so you'll want to check for <= 0.

So you were probably after something like this:

def get_side(n):
    side = int(input(f'Enter Side Length #{n}: '))
    while side <= 0:
        side = int(input(f'Try Again! Enter Side Length #{n}: '))
    return side


a = get_side(1)
b = get_side(2)
c = get_side(3)

Note that a string like f'number {n}' automatically replaces the {n} part with the value of n when it is evaluated.

n has the right value every time, because get_side() is called with a number, and n will have that value every time the function runs. So, the function will print the correct message for the three numbers.

However, those three last lines still look very repetitive, you could also:

a, b, c = (get_side(n) for n in range(1, 4))

range(1, 4) will loop from 1 up to (but not including) 4, and in the expression above, get_side(n) will be called with those values, so you get three values, which Python can assign to a, b, and c. In one go.

So the solution would be:

def get_side(n):
    side = int(input(f'Enter Side Length #{n}: '))
    while side <= 0:
        side = int(input(f'Try Again! Enter Side Length #{n}: '))
    return side


a, b, c = (get_side(n) for n in range(1, 4))
print(f'User entered {a}, {b} and {c}')

And the user interaction could look something like:

Enter Side Length #1: 2
Enter Side Length #2: -1
Try Again! Enter Side Length #2: 0
Try Again! Enter Side Length #2: 4
Enter Side Length #3: 8
User entered 2, 4 and 8
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Hi Thank you so much for your response! I do have a couple questions about it. What does the f before the text mean? Also if I wanted to call on the values for the sides that were inputted by the user in a different function what would I use? could I just use a,b,c? – Sariti Feb 20 '22 at 19:33
  • The `f` before a string makes it an "f-string", as indicated in the answer: "a string like `f'number {n}'` automatically replaces the {n} part with the value of n when it is evaluated.", see [documentation for more details](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings). And yes, after those assignments `a = ...` etc. those variables like `a` will have those values, and you can pass them to other functions. You should probably find a tutorial or two about variables and function parameters - getting the basics really helps learning Python a lot faster. – Grismar Feb 21 '22 at 03:56
  • If you feel the above answers your question, please click the checkmark to indicate your question is no longer unanswered, so others can find the answer, and people don't waste time thinking about more answers. – Grismar Feb 21 '22 at 03:56