How to fix : expected at most 1 argument, got 3 [duplicate] - python

This question already has answers here:
Error - input expected at most 1 argument, got 3
(2 answers)
Closed 3 years ago.
I am trying to ask the user about their favorite subject, but I get the following error message:
Traceback (most recent call last):
File "C:\Users\BillyG\Documents\Revision\ICT\Challenge 5.py", line 2, in
module
favesub = input("Hello what is your favourite subject", firstname, "?")
TypeError: input expected at most 1 arguments, got 3
The code is:
firstname=input("What is your name: ")
favesub = input("Hello what is your favorite subject", firstname, "?")
print ("I love ", favesub, "aswell")

input expects a single string, so unlike print, where you can append multiple arguments and the string will be parsed as is, you have to format the string yourself. For Python 3.6 and higher, user input(f"Hello what is your favourite subject {firstname}?") or input("Hello what is your favourite subject {}?".format(firstname)) if you're using an older version of Python 3.

input() only takes one argument, but you're providing 3.
Try
input(f"Hello what is your favourite subject {firstname}?")

You can concatenate the strings simply using + while asking for user input in the second line. Currently you are passing it three arguments separated by a comma.
firstname=input("What is your name: ")
favesub = input("Hello what is your favourite subject " + firstname + "?")
print ("I love ", favesub," aswell")
# What is your name: Donald
# Hello what is your favourite subject Donald?Politics
# I love Politics aswell

Try this,
favesub = input("Hello what is your favourite subject"+firstname+"?")

problem here is that input() method can only take atmost one argument, whereas you are passing 1.
firstname = input("What is your name: ")
message = "Hello what is your favourite subject "+firstname+" ?"
favesub = input(message)
print ("I love ",favesub," aswell")

Related

How do I keep input code on the same line as the print code [duplicate]

This question already has answers here:
Python: "Print" and "Input" in one line [duplicate]
(3 answers)
Closed 2 years ago.
Completely new to coding and this site, so I know this is going to seem like a silly or extremely obvious question. I've tried looking on the site for answers, but I don't know if the answers already given would work on this, or I simply don't know how to input the answers given into my code.
I want the "years old" print to appear right after you input the age, but on the same line.
name = input ("Hello, my name is ")
age = input("I am ")
print ("years old")
job = input("I am a ")
Maybe you want something like this:
name = input("Enter your name : ")
age = input("Enter your age : ")
print(f"My name is {name} and I am {age} years old")
Try something like
name = input ("Name: ")
age = input("Age: ")
print ("Hello, My name is {}, I am {} years old".format(name, age))

NameError when assigning input() to variable (Python2.7.11) [duplicate]

This question already has answers here:
Differences between `input` and `raw_input` [duplicate]
(3 answers)
Closed 3 years ago.
I get a name error even though I'm just trying to put a string into a variable.
I am trying to do this on Python 2.7.11. Does anyone have anything that helps? Upgrading Python is not an option for me.
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result = (input("enter a phrase you want encrypted: "))
result = translate(result)
This is the error that's shown:
Traceback (most recent call last):
File "D:\hello\encrydecry\encryption1.py", line 158, in <module
>
result = (input("enter a phrase you want encrypted: "))
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
In Python 2, when you use input(), Python interprets the input. So when you type hello, hello is interpreted as a variable, and you're essentially doing result = hello. Hence the error NameError: name 'hello' is not defined.
One option is to simply type the input between quotes, so it will be interpreted as a string: 'hello'.
To avoid the input being interpreted altogether, you have to use raw_input() instead of input(), which doesn't interpret the user input and always returns a string:
result = raw_input("enter a phrase you want encrypted: ")
def translate(phrase):
translation = ""
for letter in phrase:
if letter in " ":
translation = translation + "#"
result =raw_input("enter a phrase you want encrypted: ")
result = translate(result)

running into unidentifiable syntax error [duplicate]

I have a simple Python question that I'm having brain freeze on. This code snippet works. But when I substitue "258 494-3929" with phoneNumber, I get the following error below:
# Compare phone number
phone_pattern = '^\d{3} ?\d{3}-\d{4}$'
# phoneNumber = str(input("Please enter a phone number: "))
if re.search(phone_pattern, "258 494-3929"):
print "Pattern matches"
else:
print "Pattern doesn't match!"
Pattern does not match
Please enter a phone number: 258 494-3929
Traceback (most recent call last):
File "pattern_match.py", line 16, in <module>
phoneNumber = str(input("Please enter a phone number: "))
File "<string>", line 1
258 494-3929
^
SyntaxError: invalid syntax
C:\Users\Developer\Documents\PythonDemo>
By the way, I did import re and tried using rstrip in case of the \n
What else could I be missing?
You should use raw_input instead of input, and you don't have to call str, because this function returns a string itself:
phoneNumber = raw_input("Please enter a phone number: ")
In Python version 2.x, input() does two things:
Reads a string of data. (You want this.)
Then it evaluates the string of data as if it were a Python expression. (This part is causing the error.)
The function raw_input() is better in this situation because it does #1 above but not #2.
If you change:
input("Please enter a phone number: ")
to read:
raw_input("Please enter a phone number: ")
you'll eliminate the error of the phone number not being a valid Python expression.
The input() function has tripped up so many people learning Python that starting with Python versions 3.x, the designers of the language removed the extra evaluation step. This makes input() in versions 3.x behave the same as raw_input() in versions 2.x.
See also a helpful wikibooks article.
The input() function actually evaluates the input that's typed into it:
>>> print str(input("input: "))
input: 258238
258238
>>> print str(input("input: "))
input: 3**3 + 4
31
It's trying to evaluate '258 494-3929' which is invalid Python.
Use sys.stdin.readline().strip() to do your read.
input() calls eval(raw_input(prompt)), so you want phoneNumber = raw_input("Please enter a phone number: ").strip()
See also http://docs.python.org/library/functions.html#input and http://docs.python.org/library/functions.html#raw_input

Simple python code is not working [duplicate]

This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 5 years ago.
The following code is not working:
person = input('Enter your name: ')
print('Hello', person)
Instead of printing Hello <name> it is giving me the following traceback:
Traceback (most recent call last):
File "C:/Users/123/Desktop/123.py", line 1, in <module>
person = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'd' is not defined
To read strings you should use:
person = raw_input("Enter your name: ")
print('Hello', person)
When you use input it reads numbers or refers to variables instead. This happens when you are using Python 2.7 or below. With Python 3 and above you have only input function.
Your error states that you entered "d" which is a variable not declared in your code.
So if you had this code instead:
d = "Test"
person = input("Enter your name: ")
print('Hello', person)
And you type now "d" as name, you would get as output:
>>>
('Hello', 'Test')
What is the error?
You used this:
person = input('Enter your name: ')
You should have used this:
person = raw_input('Enter your name: ')
Why these are different
input tries to evaluate what is passed to it and returns the value, whereas raw_input just reads a string, meaning if you want to just read a string you need to use raw_input
In Python 3 input is gone, and raw_input is now called input, although if you really want the old behaviour exec(input()) has the old behaviour.

How to define a variable inside the print function?

I am a newbie in this field, and I am trying to solve a problem (not really sure if it is possible actually) where I want to print on the display some information plus some input from the user.
The following works fine:
>>> print (" Hello " + input("tellmeyourname: "))
tellmeyourname: dfsdf
Hello dfsdf
However if I want to assign user's input to a variable, I can't:
>>> print (" Hello ", name = input("tellmeyourname: "))
tellmeyourname: mike
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
print (" Hello ", name = input("tellmeyourname: "))
TypeError: 'name' is an invalid keyword argument for this function
I have researched inside here and other python documentation, tried with %s etc. to solve, without result. I don't want to use it in two lines (first assigning the variable name= input("tellmeyourname:") and then printing).
Is this possible?
Starting from Python 3.8, this will become possible using an assignment expression:
print("Your name is: " + (name := input("Tell me your name: ")))
print("Your name is still: " + name)
Though 'possible' is not the same as 'advisable'...
But in Python <3.8: you can't. Instead, separate your code into two statements:
name = input("Tell me your name: ")
print("Your name is: " + name)
If you often find yourself wanting to use two lines like this, you could make it into a function:
def input_and_print(question):
s = input("{} ".format(question))
print("You entered: {}".format(s))
input_and_print("What is your name?")
Additionally you could have the function return the input s.
no this is not possible. well except something like
x=input("tell me:");print("blah %s"%(x,));
but thats not really one line ... it just looks like it

Categories