samedi 13 mars 2021

How Can I make the program rerun when the user inputs y?

print("Welcome to The pyCalc")
print("For Addition press 1")
print("For Multiplication press 2")
print("For Subtraction press 3")
print("For Division press 4")
flag = 'y'
while flag == 'y':
    x = float(input("Enter your Choice(1-4): "))
    if x == 1:
        a = float(input("Enter 1st Value: "))
        b = float(input("enter 2nd Value: "))
        c = a+b
        print("Sum: ", int(c))
    elif x == 2:
        a = float(input("Enter 1st Value: "))    
        b = float(input("Enter 2nd Value: "))
        c = a*b 
        print("Product: ", int(c))
    elif x == 3:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a - b
        print("Difference: ", int(c))
    elif x == 4:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a // b
        print("Quotient: ", c)
    else:
        print("error !")
        flag = print(input("Do you want to calculate more(y/n)? : ", y))
    if flag == y:
        continue    

Hi, I'm actually new to Python so not really familiar with even basic concepts. so in this calculator program, after the first run, i want the code to rerun when the user gives an input of y. According to my knowledge, this is possible using a while loop in which we put the main code in its body and give a condition using if statement and the secondly by using the continue statement. Can somebody explain whats wrong here. p.s. indentation may be wrong in some cases.

Aucun commentaire:

Enregistrer un commentaire