Попытка сделать цикл, который останавливается на пользовательском вводе - PullRequest
0 голосов
/ 10 ноября 2019

Я пытаюсь создать калькулятор, который либо перезапустит скрипт, либо остановит его, используя «продолжить» и «прервать»Тем не менее, когда я пытаюсь запустить его, он говорит, что продолжение не в цикле, может кто-нибудь помочь, пожалуйста?

Вот код:

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")

1 Ответ

0 голосов
/ 10 ноября 2019

Ниже работает для меня, @busybear прав, отступ был выключен.

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")
...