Henry
Henry

Reputation: 31

Putting a variable value into a input's string

Is there anyway to have a variable be in the string of an input?

score = float(input("Test", grade, "-- Enter score: "))

I keep getting:

TypeError: input expected at most 1 arguments, got 3

Upvotes: 0

Views: 216

Answers (3)

maze
maze

Reputation: 940

Your error is because the input function received more than 1 argument. It received:

  1. "Test"
  2. grade
  3. "-- Enter score: "

You need to combine those three elements into one, the best way would be using a formatter (%), allowing Python to interpret it as one string:

score = float(input("Test %d -- Enter score: " % grade))

Upvotes: 0

Naveen Agarwal
Naveen Agarwal

Reputation: 1036

You can use % or format to put variable into string:

score = float(input("Test %s -- Enter score: " % grade))

or

score = float(input("Test {} -- Enter score: ".format(grade)))

Upvotes: 0

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15390

You are passing 3 strings, should be only one. You're incorrectly concatenating string. Use format for that

score = float(input("Test {} -- Enter score: ".format(grade)))

Upvotes: 1

Related Questions