我是Python的初学者,我认为制作一个简单的计算器是个好主意,一开始它应该询问用户想要的操作。然后相应地遍历。
我确实尝试过更改某些东西,但没有帮助
代码如下:
def user_input():
operation_choice = (str(input("What operation would you like to perform?: ")))
while user_input("add"):
def add_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
while user_input():
def subtract_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 - val2
print(total)
while user_input():
def multiply_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
while user_input():
def divide_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
add_numbers()
subtract_numbers()
multiply_numbers()
divide_numbers()
user_input()
答案 0 :(得分:0)
我已经自由地重新安排了一些事情。
def user_input():
operation_choice = (str(input("What operation would you like to perform?: ")))
if operation_choice == 'add':
add_numbers()
elif operation_choice == 'subtract':
subtract_numbers()
elif operation_choice == 'multiply':
multiply_numbers()
elif operation_choice == 'divide':
divide_numbers()
def add_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
def subtract_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 - val2
print(total)
def multiply_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
def divide_numbers():
val1 = int(input("Enter your first value here: "))
val2 = int(input("Enter your second value here: "))
total = val1 + val2
print(total)
user_input()
以这种方式编写,程序将执行一个操作,然后退出。如果您希望它继续要求更多操作,则将最后一个user_input()
包装在这样的无限循环中:
while True:
user_input()
然后该程序将继续运行,直到您按Ctrl-C。