Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input vs string
#1
Hi, so I'm stuck on an assignment iterating words onto seperate lines and the code works when I just fill the variable with a string, but the second I put that string into an input, it just prints the input. Could someone possibly shed some light on why that happens?

Here's the code:
 quote = "quote goes here"

   word = ""

for letter in quote:
        if letter.isalpha():
            word += letter

        else:
            if word > "":
                print(word.upper())
                word = ""
            else:
                word = ""

    print(word.upper())
And it works fine, but if I define quote with an input of the same string it just prints the string.
Thanks!
Reply
#2
Please use Python tags when posting code (see the BBCode link in my signature below for instructions). I did them for you this time.

So, are you saying that you tried quote = input("quote goes here"), and it just printed the quote? Because what input does is print the string passed to it, and then wait for the user to input a string. The user's input is then returned by input (in this case it would be assigned to the quote variable). After it prints, try typing something in and hitting return.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
How are you inputting the string from the user? I used raw_input("enter text: ") and it worked.

quote = raw_input("enter text: ")
 
word = ""
 
for letter in quote:
	if letter.isalpha():
		word += letter
	else:
		if word > "":
			print(word.upper())
			word = ""
		else:
			word = ""

print(word.upper())
Reply
#4
(Oct-15-2018, 02:01 AM)ichabod801 Wrote: Please use Python tags when posting code (see the BBCode link in my signature below for instructions). I did them for you this time.

So, are you saying that you tried quote = input("quote goes here"), and it just printed the quote? Because what input does is print the string passed to it, and then wait for the user to input a string. The user's input is then returned by input (in this case it would be assigned to the quote variable). After it prints, try typing something in and hitting return.

Ya my bad, I'm still pretty new to this.

I did what you said and nothing happens. It works when I just assign the string to quote though.

(Oct-15-2018, 02:23 PM)marienbad Wrote: How are you inputting the string from the user? I used raw_input("enter text: ") and it worked.
 quote = raw_input("enter text: ") word = "" for letter in quote: if letter.isalpha(): word += letter else: if word > "": print(word.upper()) word = "" else: word = "" print(word.upper())
I'm using input(). Tried using raw_input and it throws off NameError: "name 'raw_input' is not defined".
quote = raw_input("Wheresoever you go, go with all your heart")
word = ""

for letter in quote:
        if letter.isalpha():
            word += letter
        else:
          if word[0].lower() >= "":
            print(word.upper())
            word = ""
          else:
            word = ""
print(word.upper())
Reply
#5
ok, sorry, I am still on python 2.7. raw_input is now input in python 3, and like I said, it worked for me, so I can't see why it doesn't work for you. Try putting quotes around the text you are inputting.
Reply
#6
Then I'm not sure what's going on. It works for me with raw_input in 2.7 and input in 3.6.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding string numbers, while loop and exit without input. Jose 11 7,456 Apr-15-2020, 08:34 AM
Last Post: Jose
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,539 Feb-27-2020, 09:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020