dimanche 10 novembre 2019

Trying to make a loop that stops on user input

I'm trying to make a calculator that will either restart the script or stop it using "continue" and "break". Yet when I try to run it, it says continue is not in loop, can someone help please?

Here's the code:

import os
import sys

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

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

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

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


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

choice = input("Enter choice(1/2/3/4): ")
if choice > "4": # (REFER TO TAG 1) : Seeing if this would work ( something to compare to )
    while True:
        print ("Invalid Input")
        answer = input('Run again? (y/n): ')
    if answer in ('y', 'n'):
            if answer == "y":
                continue
            if answer == "n":
                break

num1 = (input("Enter first number: ")) # Got rid of float() before input
num2 = (input("Enter second number: ")) # Got rid of float() before input
if choice == "1": # Changed single speech mark to double.
    print(num1,"+",num2,"=", add(num1,num2))
elif choice == "2": # Changed single speech mark to double.
    print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == "3": # Changed single speech mark to double.
    print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == "4": # Changed single speech mark to double.
    print(num1,"/",num2,"=", divide(num1,num2))
else:
    print("Invalid Input")
``

Aucun commentaire:

Enregistrer un commentaire