Reputation: 159
I am trying to generate a story using python. For this I'm trying to take the input from the users for some questions. The scenario I'm trying to get is that whenever the user enters the input, then it is displayed on the output screen in capital letters.
But what happens is that the text is diplayed in small letters.
Here is the sample of the code
message1 = input(" my name is: ")
message2 = input(" i am from: ")
message3 = input(" i love to eat: ")
print( " My name is " + message1.upper() + " I am from " + message2.upper() + " I love to eat " + message3.upper())
I expect My name is SANDEEP
when the user enters sandeep
, but I get sandeep
.
Upvotes: 1
Views: 781
Reputation: 26
You can use this following code for console app. I am converting the input after reading it whole. But you can do the job quiet easily and get your desired result(Converting every character as you enter to uppercase) when you'll implement it in web applications( by using html and javascript there).
import os
message1 = input(" MY NAME IS: ")
os.system('cls')
message1="MY NAME IS : "+message1.upper()
res=message1+"\n"
message2 = input(res+"I AM FROM: ")
os.system('cls')
res+="I AM FROM : "+message2.upper()+"\n"
message2="I AM FROM : "+message2.upper()
message3 = input(res+"I LOVE TO EAT: ")
os.system('cls')
res+="I LOVE TO EAT : "+message3.upper()+"\n"
message3="I LOVE TO EAT: "+message3.upper()
print(res+"\n\n\n"+ message1 +"\t"+message2+"\t"+message3)
Upvotes: 1
Reputation: 20738
So, this will only work on Linux systems and only with ASCII characters:
import termios
import sys
def uppercase_input(prompt):
sys.stdout.write(prompt)
sys.stdout.flush()
old = termios.tcgetattr(sys.stdout.fileno())
new = old[:]
new[1] |= termios.OLCUC
termios.tcsetattr(sys.stdout.fileno(), termios.TCSANOW, new)
try:
return input().upper()
finally:
termios.tcsetattr(sys.stdout.fileno(), termios.TCSANOW, old)
result = uppercase_input("all uppercase? ")
print("yes: {}".format(result))
This uses some interesting ancient settings to convert I/O to/from uppercase from the old times when there were terminals only supporting upper-case ASCII.
From man tcsetattr
:
OLCUC (not in POSIX) Map lowercase characters to uppercase on output.
So this solution is not really portable.
For a fully portable solution, you would need to implement echoing yourself.
Upvotes: 0