程序无法完成整个脚本

时间:2019-11-13 00:29:12

标签: syntax python-3.6

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

但是,如果您输入“ A”或“ B”,它会起作用,而当您输入“符号”或操作时,它也会起作用。但是,在用户输入符号后,它将完成程序并退出代码。

我需要有关这种情况的一些反馈意见:

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

 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


1 个答案:

答案 0 :(得分:0)

您也需要调用函数。检查最后一行。同时缩进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()