1

so i'm new to python and to coding in general and I have some difficulties about a use of loops. Here is the following code (prints are in french but I guess no translation is needed)

n = input ("Saissisez le nombre totale de bonnes notes obtenues")
n = int(n)
if n > 0:
    a = n+1
    b = n*a
    c = b/2
    print("Vous avez accumulez au total la somme de :", c, "€")
    print("Bien joué :)")
    input()

else:
    print("Vous n'avez pas rentré de nombre entiers correctes. Veuillez réessayer.")
    n = input ("Saissisez le nombre totale de bonnes notes obtenues")

So what I want to do is to create a loop for the "else" in order to ask the question again (the input) until a number superior to 0 is selectioned. It would be also cool if it doesn't "error" when I type a letter instead of a number for example.

Thanks in advance ! (and sorry for this stupid question) By the way the input in the "if" is to avoid the window to close instantly.

2 Answers2

0

To ensure the user gives you an interger greater than 0:

is_integer = False
n = ''
while not is_integer:
    n = input("Saissisez le nombre totale de bonnes notes obtenues")

    if type(n) == int and n > 0:
        is_integer = True
    else:
        print('Not an integer greater than 0')
Nick Dima
  • 348
  • 1
  • 10
0

Your best bet is to define your overall procedure as a function and have it call itself.

def myProcedure():
    n = input('Give me a number')
    ...
    if repeat_condition:
        myProcedure()
    else:
        finish up

As for your number error, use a try block

try:
    float(n)
    ...
except ValueError:
    myProcedure()
Simon
  • 855
  • 9
  • 24