我的程序已接近完成,但我似乎无法允许......
"您想进行更多计算吗?输入(Y)表示是,或输入任何""其他字符表示否。 "
...在没有先输入我的选择(例如" Y"或任何其他角色")的情况下多次输出。
我真的很感激一些帮助!
"""My First Program!!!"""
# Modules:
import time # Provides time-related functions
# Delayed text to give it a "Turing" feel
def calculator_print(*args, delay=1):
print(*args)
time.sleep(delay)
# Operations:
def add(num1, num2):
# Returns the sum of num1 and num2
return num1 + num2
def sub(num1, num2):
# Returns the difference of num1 and num2
return num1 - num2
def mul(num1, num2):
# Returns the product of num1 and num2
return num1 * num2
def div(num1, num2):
# Returns the quotient of num1 and num2
try:
return num1 / num2
except ZeroDivisionError:
# Handles division by zero
calculator_print("Division by zero cannot be done. You have broken the universe. Returning zero...")
return 0
def exp(num1, num2):
# Returns the result of num1 being the base and num2 being the exponent
return num1 ** num2
# Run operational functions:
def run_operation(operation, num1, num2):
# Determine operation
if operation == 1:
calculator_print("Adding...\n")
calculator_print(num1, "+", num2, "=", add(num1, num2))
elif operation == 2:
calculator_print("Subtracting...\n")
calculator_print(num1, "-", num2, "=", sub(num1, num2))
elif operation == 3:
calculator_print("Multiplying...\n")
calculator_print(num1, "*", num2, "=", mul(num1, num2))
elif operation == 4:
calculator_print("Dividing...\n")
calculator_print(num1, "/", num2, "=", div(num1, num2))
elif operation == 5:
calculator_print("Exponentiating...\n")
calculator_print(num1, "^", num2, "=", exp(num1, num2))
else:
calculator_print("I don't understand. Please try again.")
def main():
# Ask if the user wants to do more calculations or exit:
def restart(response):
# uses "in" to check multiple values,
# a replacement for (response == "Y" or response == "y")
# which is longer and harder to read.
if response in ("Y", "y"):
return True
else:
calculator_print("Thank you for calculating with me!")
calculator_print("BEEP BOOP BEEP!")
calculator_print("Goodbye.")
return False
# Main functions:
# Title Sequence
calculator_print('\n\nThe Sonderfox Calculator\n\n')
calculator_print(' ----LOADING----\n\n')
calculator_print('Hello. I am your personal calculator. \nBEEP BOOP BEEP. \n\n')
while True: # Loops if user would like to restart program
try:
# Acquire user input
num1 = (int(input("What is number 1? ")))
num2 = (int(input("What is number 2? ")))
operation = int(input("What would you like to do? \n1. Addition, 2. Subtraction, 3. Multiplication, "
"4. Division, 5. Exponentiation \nPlease choose an operation: "))
except (NameError, ValueError): # Handles any value errors
calculator_print("Invalid input. Please try again.")
return
run_operation(operation, num1, num2)
# Ask if the user wants to do more calculations or exit:
restart_msg = input("Would you like to do more calculations? Enter (Y) for yes, or any "
"other character for no. ")
if not restart(str(input(restart_msg))): # uses the function I wrote
return
main()
答案 0 :(得分:0)
您要求输入输入。 restart_msg = input()然后你输入(restart_msg)。
另请注意,不需要使用str()将输入转换为字符串,因为在python3中input()已经返回一个字符串。
答案 1 :(得分:0)
如果这真的是你的第一个节目,那真是令人印象深刻!
所以,我已经粘贴了我们想要关注的代码:
restart_msg = input("Would you like to do more calculations? Enter (Y) for yes, or any other character for no. ")
if not restart(str(input(restart_msg))): # uses the function I wrote
return # Stop the program
在第一行中,计算机会提示您输入“是否要进行更多计算?” (等等)。然后它将第一个输入存储在变量restart_msg
中。然后,在第二行中,您调用restart(str(input(restart_msg)))
。由于它包含对input()
的调用并将restart_msg
作为唯一参数传递,因此计算机会通过输出您刚刚输入的内容来提示输入。它将该条目存储在字符串中,并将其传递给restart()
。
这似乎是你在第二行的意图:
if not restart(str(restart_msg)):
这样,计算机将您输入的第一个输入转换为字符串,方法是将其传递给str()
,并将其传递给重启函数。
这是一个非常雄心勃勃的项目,祝你好运!