Hello

I'm writing a python program and need some help.
Firstly, is there anyway to include variables in an input prompt?
For eg.
[variable1 = 4
variable 2= 3
variable= raw_input("Please enter a number between", variable 1, "and", variable2) ]

Also, if data is being appended to the end of a text file, when reading and printing the data, is there anyway to put a new line between each piece of data.
For eg.
If the text in the text file looked like this: John, 3Sam, 4
Can you split after the number and display the next piece on the next line?
My code treading and printing this section is this:
[scores = open("scores.txt", 'r')
print scores.read()
scores.close()]

Thankyou for any help, it is appreciated.

Recommended Answers

All 4 Replies

Your first problem can be solved with string formatting.

variable1 = "Redyugi"
print "My name is %s." % (variable1)

Your second problem can be fixed easily.
When you are appending text to your file, add the string "\n" to the end of each piece. That way the data will be separated by a new line. Then you can just do this.

f = open("file.txt", "r")
x = f.read()
f.close()
y = x.split("\n")
for i in y:
    print i

Thankyou so much, but I'm still struggling with the first problem.
This code is the best I could do but I'm sure it's far from right.
Thankyou for your help.

smallestNum = 1
largestNum = 42
userNumber = input("Please enter a number between %s." % (smallestNum) "and %s." % (largestNum))

It's

"Please enter a number between %s and %s." % (smallestNum, largestNum)

Thankyou for all your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.