Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试创建一个在用户输入时停止的循环_Python_If Statement_While Loop - Fatal编程技术网

Python 尝试创建一个在用户输入时停止的循环

Python 尝试创建一个在用户输入时停止的循环,python,if-statement,while-loop,Python,If Statement,While Loop,我正在尝试制作一个计算器,它要么重新启动脚本,要么使用“继续”和“中断”停止脚本。然而当我试着运行它时,它说continue不在循环中,有人能帮忙吗 代码如下: 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

我正在尝试制作一个计算器,它要么重新启动脚本,要么使用“继续”和“中断”停止脚本。然而当我试着运行它时,它说continue不在循环中,有人能帮忙吗

代码如下:

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

下面是我的作品,@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")

似乎是
如果回答为('y','n'):
需要缩进编辑,因为
继续
不在循环中。确实有一个
while
循环,但是下面的
if answer…
终止了循环。也许你的缩进掉了。谢谢忙碌的人,成功了。Zvone我现在查一下。因为True在错误的地方,所以我不得不更新代码。