[英]user_input() takes 0 positional arguments but 1 was given error
I am a beginner to Python and I thought it was a good idea to make a simple calculator, at the start it should ask the user for which operation it wants. 我是Python的初学者,我认为制作一个简单的计算器是个好主意,一开始它应该询问用户所需的操作。 Then loop through accordingly.
然后循环遍历。
I did try changing certain things but it didn't help 我确实尝试过更改某些东西,但没有帮助
Here's the code: 这是代码:
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()
I've taken the liberty of rearranging a few things. 我已经自由地重新安排了一些事情。
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()
Written this way, the program will perform one operation and then exit. 以这种方式编写的程序将执行一个操作,然后退出。 If you want it to keep asking for more operations, then wrap the last
user_input()
in an infinite loop like this: 如果您希望它继续要求更多操作,则将最后一个
user_input()
包裹在这样的无限循环中:
while True:
user_input()
Then the program will go on running until you press Ctrl-C. 然后,程序将继续运行,直到您按Ctrl-C。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.