Home > Mobile >  Calling getpass() says too many positional arguments when trying to print prompt
Calling getpass() says too many positional arguments when trying to print prompt

Time:11-10

I am very new to Python (Only a couple weeks) and I'm trying to make a rock paper scissors game for Replits 100 days of code. My previous game was very simple and worked fine but it wont fly as a template for this lesson so I've started from scratch but I'm having trouble.

from getpass import getpass as hinput
ch1 = hinput("Enter",p1,"choice: ")

I keep getting this error after the players enter the names

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    ch1 = hinput("Enter",p1,"choice: ")
TypeError: unix_getpass() takes from 0 to 2 positional arguments but 3 were given

CodePudding user response:

You're using getpass like print. print is sort of special in Python, as it's written to take any number of arguments. You can write functions like that yourself, but it's not the default. And getpass is written to only accept a single string as an argument (and an output stream, but we don't need that for our purposes).

You'll need to build the string yourself. That can be done with concatenation

hinput("Enter "   p1   " choice: ")

or format strings

hinput("Enter %s choice: " % p1)       # Old (printf) style
hinput("Enter {} choice: ".format(p1)) # New style

or, on Python 3.5 and newer, f-strings

hinput(f"Enter {p1} choice: ")

CodePudding user response:

You should use

hinput(f"Enter {p1} choice: ")

This makes use of f-strings, which, for Python 3.5 , allow you to format variables within a string. For instance:

a = 4
b = 3
print(f"a   b = {a b}")
>>> a   b = 7

Note that "Enter",p1,"choice: " is a tuple with 3 elements, and not a single string.

  • Related