首页 > 解决方案 > 程序没有完成整个脚本

问题描述

我正在启动一个程序,您必须在其中选择一种模式、操作,然后它会为您计算。它仍在进行中。

但是,如果您输入“A”或“B”,我遇到了一个问题,当您输入“符号”或操作时它也可以工作。但是,在用户输入符号后,它会完成程序并退出代码。

我需要一些关于这种情况的反馈:

我的最终目标是让程序也要求用户输入他们想要计算的数字,但程序不会要求用户输入。我觉得好像这个问题必须处理浮点数(输入,elifs'(如果然后其他),或者它是如何卡在函数的参数中的。

 import fractions
print("Welcome to Advanced Calculator Version 1.01")
module = input("Enter a mode, for example; 1NUMBER_OPERATIONS [A], or 2NUMBER_OPERATIONS [B]")
if module == "A":
    operation = input('Enter an operation, for example; sq_rt,fraction,percent,round')
elif module == "B":
    operation = input('Enter an operation, for example; -,+,*,/,^')


def doublecheck():
#OPERATION|||MODESA
    if operation == "-":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 - num2
        print(result)
    elif operation == "+":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 + num2
        print(result)
    elif operation == "*":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 * num2
        print(result)
    elif operation == "/":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 / num2
        print(result)
    elif operation == "^":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1^num2
        print(result)
    elif operation == "sq_rt":
        num1 = float(input("Enter a number: "))
        num_sqrt = num1 ** 0.5
        print('The square root of %0.3f is %0.3f' % (num1,num_sqrt))
    elif operation == "fraction":
        num1 = float(input("Enter a number: "))
        str(fractions.Fraction(num1))
    elif operation == "percent":
        num1 = float(input("Enter a number: "))
        v = 100
        percent= num1 * v
        print(percent + "%")
    elif operation == "round":
        num1 = float(input("Enter a number: "))
        round(num1)
return 0


标签: syntaxpython-3.6

解决方案


你也需要调用你的函数。检查最后一行。还要缩进你的 return 0 语句。

import fractions
print("Welcome to Advanced Calculator Version 1.01")
module = input("Enter a mode, for example; 1NUMBER_OPERATIONS [A], or 2NUMBER_OPERATIONS [B]")
if module == "A":
    operation = input('Enter an operation, for example; sq_rt,fraction,percent,round')
elif module == "B":
    operation = input('Enter an operation, for example; -,+,*,/,^')


def doublecheck():
#OPERATION|||MODESA
    if operation == "-":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 - num2
        print(result)
    elif operation == "+":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 + num2
        print(result)
    elif operation == "*":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 * num2
        print(result)
    elif operation == "/":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1 / num2
        print(result)
    elif operation == "^":
        num1 = float(input("Enter a number: "))
        num2 = float(input("Enter another number: "))
        result= num1^num2
        print(result)
    elif operation == "sq_rt":
        num1 = float(input("Enter a number: "))
        num_sqrt = num1 ** 0.5
        print('The square root of %0.3f is %0.3f' % (num1,num_sqrt))
    elif operation == "fraction":
        num1 = float(input("Enter a number: "))
        str(fractions.Fraction(num1))
    elif operation == "percent":
        num1 = float(input("Enter a number: "))
        v = 100
        percent= num1 * v
        print(percent + "%")
    elif operation == "round":
        num1 = float(input("Enter a number: "))
        round(num1)
    return 0

doublecheck()

推荐阅读