abobex abobex - 2 years ago 75
Python Question

print text with stored user inputs, wihout ([''], ', ')

Python 2.7
I am asking the user information and then store it like this:
user_Name = []

I print out questions that looks like this:
Q_2 = user_Name, ", How old are you ?"

the output of print Q_2 is:
(['abobex'], ', How old are you ?')

All the inputs the user input is stored and i can print out all of them, but it prints out ([''], ', ?') instead of just: abobex, How old are you ?

I have tested changed alot within the print and some solutions did not print out everything correct.
I bet it is a easy problem, im new to python.

a sample of the code:

user_Name = []
Q_2 = user_Name, ", How old are you ?"
print Q_2
input = raw_input()
user_Age = input

is there something i can tell raw_input() to do or the print statement ?

Answer Source

It's displaying the username with brackets because user_Name is an array containing a string. The parentheses are there because Q_2 is being assigned a tuple value - to correctly concatenate strings, use the plus sign, not a comma.

If you want to get the actual username to print in the string, and assign a string instead of a tuple to Q_2, you'd simply have:

Q_2 = user_Name[0] + ", how old are you?".

However, there are a few unnecessary things in your code. For one, you can pass the question string to raw_input like: input = raw_input(Q_2)

In fact, your code as posted could be condensed to this:

user_Age.append(raw_input(user_Name[0] + ", how old are you?"))

It's also not clear why you're appending input to user_Age, which I assume is a list, but then replacing that list with input - you're essentially throwing away your list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download