2

I'm trying to auto-restart this program, language is Italian.

import sys
import os
#semplice calcolatrice 

def addizione(x,y):
    return x + y

def sottrazione(x,y):
    return x - y

def moltiplicazione(x,y):
    return x * y

def divisione(x,y):
    return x/y

def potenza(x,y):
    return pow(x, y)

#imput dall'utente
print("Seleziona l'operazione.")
print("1.addizione")
print("1.sottrazione")
print("1.moltiplicazione")
print("1.divisione")
print("1.potenza")

choice=input("Inserisci 1/2/3/4/5: ")
num1=int(input("Inserisci il primo numero: "))
num2=int(input("Inserisci il secondo numero: "))

if choice == '1':
    print(num1, "+", num2,"=", addizione(num1,num2))

elif choice == '2':
    print(num1, "-", num2,"=", sottrazione(num1,num2))

elif choice == '3':
    print(num1, "*", num2,"=", moltiplicazione(num1,num2))

elif choice == '4':
    print(num1, "/", num2,"=", divisione(num1,num2))

elif choice == '5':
    print(num1, "^", num2,"=", potenza(num1,num2))

else:
    print("Input invalido")

choice=input("Nuovo calcolo: 1, Chiudi: 2 > ")
if choice == '1':
    os.execv(__file__, sys.argv)
elif choice == '2':
    exit()

I searched in other answer and i found this os.execv(__file__, sys.argv). It shows me this error at the end if I try to "restart" the calculator Traceback (most recent call last): File "C:\Users\ruben\eclipse-workspace\prove\test\calcolatrice.py", line 57, in <module> os.execv(__file__, sys.argv) OSError: [Errno 8] Exec format error.

Using Python in Eclipse with PyDev, thanks for the help!

  • 4
    All you want is a `while` loop. – roganjosh Jan 18 '20 at 14:08
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – AMC Jan 18 '20 at 16:21

1 Answers1

1

i suggest you use a while loop:

iteration= 0
while iteration != 1:
    def addizione(x,y):
        return x + y

    def sottrazione(x,y):
        return x - y

    def moltiplicazione(x,y):
        return x * y

    def divisione(x,y):
        return x/y

    def potenza(x,y):
        return pow(x, y)

    #imput dall'utente
    print("Seleziona l'operazione.")
    print("1.addizione")
    print("1.sottrazione")
    print("1.moltiplicazione")
    print("1.divisione")
    print("1.potenza")

    choice=input("Inserisci 1/2/3/4/5: ")
    num1=int(input("Inserisci il primo numero: "))
    num2=int(input("Inserisci il secondo numero: "))

    if choice == '1':
        print(num1, "+", num2,"=", addizione(num1,num2))

    elif choice == '2':
        print(num1, "-", num2,"=", sottrazione(num1,num2))

    elif choice == '3':
        print(num1, "*", num2,"=", moltiplicazione(num1,num2))

    elif choice == '4':
        print(num1, "/", num2,"=", divisione(num1,num2))

    elif choice == '5':
        print(num1, "^", num2,"=", potenza(num1,num2))

    else:
        print("Input invalido")

    choice=input("Nuovo calcolo: 1, Chiudi: 2 > ")
    if choice == '1':
        pass
    elif choice == '2':
        iteration = iteration + 1
        #this can be removed:
        exit()

the same can be done with boolean instead of the iteration i used, comment here if you would like me to change it to that (it is technically better, but a bit more confusing).

wondercoll
  • 339
  • 1
  • 4
  • 15
  • Thank you for the comment, I modified the code with the while loop but this time it doesn't work, when I run nothing happen in the console, i can just write numbers and letters – Ruben Petronio Jan 18 '20 at 15:16
  • @RubenPetronio That's probably because of the current design of your program. Take a look at the question I shared in my comment :) – AMC Jan 18 '20 at 16:37