0

As an intermediate python developer, I have attempted to solve a problem which simulates a virtual library. I essentially need to ask the user for their name and the number of books they have read.

My input must look something like this:

name = str(input("Enter name: ")))
booksRead = int(input("Number of books", *name variable* "read: "))

Unfortunately, I cannot seem to find any way to reference my name variable within my booksRead variable (obviously python does not allow you to reference variables within input prompters).

Is there any way I can achieve the same result?

2 Answers 2

2

use :

booksRead = int(input(f"Number of books {name} read: "))

https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals

https://docs.python.org/3/reference/lexical_analysis.html#f-strings

Sign up to request clarification or add additional context in comments.

2 Comments

f-string is def my prefered approach these days makes things look cleaner and more readable
Gotta agree with you there
2

So there is multiple possible solutions, one of them is what Loïc suggested

another option is using format:

booksRead = int(input("Number of books {:} read: ".format(name)))

another option is using %s:

booksRead = int(input("Number of books %s read: " % name))

all are equivalent

2 Comments

agreed, those options also work, but I must say I'm fond of f-string formatting :D
Completely forgot about the new string format, thanks for the help and providing some other options, much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.