1

I am new to Python and i want to know how to combine a String with all the elements of a List and then save Output in a .txt File. But the window just open and close immediately because the code is wrong. This is my code:

List = [1, 2, 3, 4, 5]

String = input("Please enter a name: ")
Output = String + List
print(Output)

f= open("Text.txt","w+")
f.write(Output + "\n")
f.close()

I am expecting to see this results:

Please enter a name: Username
Username1 Username2 Username3 Username4 Username5

And in the Text File like this:

Username1
Username2
Username3
Username4
Username5

How am i supposed to do this? If the question is not clear please let me know. My first language is not English so is hard for me to explain. Thanks for your time :)

2
  • 1
    I suggest you read some of the PEP8 style guide in order to write more readable code. python.org/dev/peps/pep-0008 Commented Apr 19, 2018 at 21:36
  • Welcome to SO. Unfortunately this isn't a discussion forum or tutorial. Please take the time to read How to Ask and the other links found on that page. Invest some time with the Tutorial practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. Commented Apr 19, 2018 at 22:22

2 Answers 2

4

I reckon you can use a list comprehension:

List = [1, 2, 3, 4, 5]

String = input("Please enter a name: ")

Output = [String + str(x) for x in List]

print(Output)

Output:

['Username1', 'Username2', 'Username3', 'Username4', 'Username5']
Sign up to request clarification or add additional context in comments.

10 Comments

Then you have another for loop which then writes each of those entries into a new line on the text file
Yucky variable naming especially for someone who is just learning the language. I suggest an edit to name them something more reasonable.
@ChadVanDeHey I agree, but direct that to OP in his question.
@ChadVanDeHey Yeha, i know that but i change the name of the variables just to make this Question easier to understand because as i said its hard for me to explain because i don't speak English very well. In my real code i have used: Chars - List | Username - String | Password - Output | Which is think is good for me. Thanks for the suggestion anyway :)
@JacobG. Hm... I don't know if i am doing anything wrong but this is not working. I get an error after i type a name in the "String". Do you know what is going on?
|
1

Following Jacob G. entry your code should look like this:

List = [1, 2, 3, 4, 5]
# input() only works for Python 3.x . Use raw_input() if using python 2.x
InputName = input("Please enter a name: ")
Usernames = [InputName + str(entry) for entry in List]
print(Usernames)

file= open("Text.txt","w+")
for username in Usernames:
   file.write(username)
file.close()

5 Comments

Aghh... I am sorry but both your code and Jacob G. code is not working. I don't know if i am doing anything wrong but after i give my input to the "InputName" the Terminal just close and the Text.txt file don't get created. I tried to type the code one by one in the Python IDLE and i get this error after i type the 2nd command: NameError: name 'username' is not defined.
And when i run the 3rd command i get this error: TypeError: unsupported operand type(s) for +: 'int' and 'str'
Ok for the first one - username cannot be found, have you put made sure that the line file.write(username) is in the for loop for username in Usernames:
@Ben10 It turns out OP was using Python 2.7 and had to use raw_input instead.
Thanks for trying to help again i really appreciate it but i have fixed the error now. The problem was that i was using Python 2.7 where you need to use raw_input and your code was for -V 3.x so i just installed the latest version of Python now and its working perfectly. Thanks a lot for helping me to get started :)

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.