syntax - 程序没有完成整个脚本
问题描述
我正在启动一个程序,您必须在其中选择一种模式、操作,然后它会为您计算。它仍在进行中。
但是,如果您输入“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
解决方案
你也需要调用你的函数。检查最后一行。还要缩进你的 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()
推荐阅读
- c - mktime() 返回大量秒数
- powershell - 在管道中显示两种方法
- ruby - 如何在 Nokogiri 中提取部分文本
- php - exec() 如何工作 - PHP 和 Wordpress
- node.js - nodejs smtp-server 电子邮件已发送但未收到
- angular - Angular CLI 版本 7.3.3 与 Typescript 版本 3.3.3333 不兼容
- c# - 创建了多个 Ninject Singleton 实例
- arrays - 运行 Apps 脚本以迭代数组时出现类型错误
- node.js - heroku 没有显示 Procfile 文件 Discord.js
- authentication - 使用 openssl 的 .p7b 文件的两种方式 SSL 身份验证