Write a program that asks the user for 15 letters, accumulates them into a single string variable, and prints the string.
@Kainui
@pranabanimesh
What have you done so far?
def main(): a = eval(input("Please enter a letter: ")) b = eval(input("Please enter a letter: ")) c = eval(input("Please enter a letter: ")) d = eval(input("Please enter a letter: ")) e = eval(input("Please enter a letter: ")) f = eval(input("Please enter a letter: ")) g = eval(input("Please enter a letter: ")) h = eval(input("Please enter a letter: ")) i = eval(input("Please enter a letter: ")) j = eval(input("Please enter a letter: ")) k = eval(input("Please enter a letter: ")) l = eval(input("Please enter a letter: ")) m = eval(input("Please enter a letter: ")) n = eval(input("Please enter a letter: ")) o = eval(input("Please enter a letter: ")) "".join(["'a','b','c','d','e','f','g','h','i','j',\ 'k','l','m','n','o'"]) main()
that is not correct. I know it!
@Kainui
Sorry, the site is slow so it didn't tell me you responded for some reason
So you know it's not right, what can you tell me about why it's wrong? What happens when you run the program?
it has to print the 15 letters in one string
when it ask for the first letter, it entered it and it says that letter is not defined
What does this code do? ` a = eval(input("Please enter a letter: "))`
I suppose [a to o] evaluates the 'letters' I am entering and prints it out like a whole word
I think the key thing we need to understand is that `eval()` and `input()` are doing two different things, can you try to explain them to me? You have the right idea I believe, but we need to change what we're doing. Can you give me an example of how you would use each of those separately by themselves, like with a simple example?
def main(): x = eval(input("enter a number: ")) b = (x * 100) print("the total is: ",b) main()
i do not learn how to use them separately
i am sure the program is much shorter than what I wrote.
I just copied and pasted everything for 15 times. It does not make sense
Ahhh ok I see. I will teach you since this is very important, and it's usually a bad idea to have eval(input("stuff")) since it can allow someone to type other code in to be evaluated! so first let's look at it from the inside out since that will hopefully make more sense. `x = input("text")` simply puts whatever text is in the quotes to the screen and lets you type in a value. That value will be stored as x in this example, or it can act as the variable itself if you want to use it immediately like you've done here. So if the next line was `eval(x)` that is the same as `eval(input("text"))` since they are the same thing. With me so far?
yes
Alright so now what's `eval()` doing? It's just evaluating whatever is in parenthesis. So in your code here ``` x = eval(input("enter a number: ")) b = (x * 100) print("the total is: ",b) ``` eval is just taking the input's value and evaluating it, so since it isn't doing anything it should be equivalent to doing this, try it out and check. ``` x = input("enter a number: ") b = (x * 100) print("the total is: ",b) ``` What eval() does then is like I said earlier, it lets you run code from in it, which includes doing mathematical functions. This can be a security hazard since someone can evaluate code that erases things on your computers while using your program.
it is just printing the number 100 times
What is?
the program you just gave me. It is printing the entered number 100 times
It's the exact same program you gave me, I didn't change anything except show that you can replace `x = eval(input("enter a number: "))` with `x = input("enter a number: ")`
i need to finish this program by 11:50 or I will lose points
x = input("enter a number: ") If I did this, it is printing the entered number 100 times. I just tried it!
Oh I'm sorry I am confusing my syntax up my bad, the problem is that x is a string and not an int, so when you do ("number" *100) it is just putting 100 copies of the string next to each other. So you should be casting it to an int with this method... `int(input("enter a number: "))` But this is not important or useful to you right now, the point I am trying to make is that eval does mathematical operations but you only need a string. Try this code out: ``` s=input("type letter: ") print(s) ``` That should make my point a little more clear and get you on the right path, sorry for the confusion!
it is working now!
can you please answer my question?
Write a program that asks the user for 15 letters, accumulates them into a single string variable, and prints the string.
``` myList = [] counter = 0 while counter < 15: myList.append(str(raw_input("Enter a string: "))) counter += 1 print myList myStr = "".join(myList) print myStr ```
@ahamed. Currently what I've given you is a way to take input and turn it into strings. But what you need is to limit the user to only entering in single characters, ask for 15 of them, and then concatenate them into a single string. I can't answer your question for you, as that's against the code of conduct, but I can help you understand what methods do.
``` myNewStr = "" counter = 0 while counter < 15: myNewStr += str(raw_input("Enter a string: ")) counter += 1 print myNewStr ```
@pranabanimesh i am getting errors, man
Remember, the input has to be characters, not a string.
i am getting errors
@ahamed. what error, did you copy paste the complete code?
yes i did! several error and i tried to fix them too! *no parenthesis on the print *mynewstr not defined
sorry, this is python 2.7, are you using python 3?
yes
please try the program here and send it to me. http://repl.it/languages/Python3
I have little knowledge on python 3. the above code works for python 2.7
@pranabanimesh give this a guy a medal and fan him
Join our real-time social learning platform and learn together with your friends!