AussieCozzie75
AussieCozzie75

Reputation: 13

Concatenation problems with strings, integers and user input

I am currently making a program to tests people's knowledge on times tables, but I am having trouble with user input. Currently, my code looks like this.

user_solution = int(input("Solve "+user_number+" * "+rand_number+": "))

The two variables are both integers, however, I have made this form of concatenation work by converting both integers to strings before running this code, and converting them back after the code is run, however, this is lengthy. My previous code looks like this.

user_solution = int(input("Solve ",user_number," * ",rand_number,": "))

This uses commas instead of plus signs for concatenation, however, when running this code it provides the error message:

TypeError: input expected at most 1 argument, got 4

I am not sure how to fix it, I would love some help.

Upvotes: 1

Views: 48

Answers (1)

Buddy Bob
Buddy Bob

Reputation: 5889

Comma concatenation is fine until it comes to using it directly in a function. Doing this, python thinks you are giving multiple arguments.

func(a,b)

so instead use f-strings

user_solution = int(input(f"Solve {user_number} * {rand_number} : "))

Upvotes: 1

Related Questions