简体   繁体   English

user_input()接受0个位置参数,但给定1个错误

[英]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.

相关问题 “采用 0 个位置参数,但给出了 1 个”错误 - "takes 0 positional arguments but 1 was given" error 输入[]出现错误:findDistance()接受0个位置参数,但给出了1个 - Getting error on input []: findDistance() takes 0 positional arguments but 1 was given 类型错误:input() 需要 0 个位置参数,但给出了 1 个 - TypeError: input() takes 0 positional arguments but 1 was given argparse错误“接受0个位置参数,但给出了2个” - argparse error “takes 0 positional arguments but 2 were given” 接受 0 个位置参数,但给出了 1 个 - takes 0 positional arguments but 1 was given 错误:choice()接受2个位置参数,但给出了4个 - error: choice() takes 2 positional arguments but 4 were given Django错误:取0位置参数但给出1 - Django error : takes 0 positional arguments but 1 was given 线程错误:get() 取 0 个位置 arguments 但给出了 1 - Threading error: get() takes 0 positional arguments but 1 was given 要求输入错误的方法的Python 3.x错误为TypeError:__init __()接受0个位置参数,但给出了1个 - Python 3.x error with methods asking for input error is TypeError: __init__() takes 0 positional arguments but 1 was given create_user() 需要 2 到 4 个位置 arguments 但 5 个被赋予 django - create_user() takes from 2 to 4 positional arguments but 5 were given django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM