Python programming: printing an input in upper case [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I can print a string in upper case but can't get it to work when i ask the user to input a sentence. I have tried this code:
sntc = str(input ("Please enter a sentence."))
str.upper (sntc)
and this:
sntc = input ("Please enter a sentence.")
str.upper (sntc)
but none of these seem to work. Any ideas?

Python strings are immutable, so string methods like str.upper() return a new string.
So in your case, you would need to do:
upper_string = sntc.upper()

You aren't modifying the string when you use .upper() because strings are immutable. You need to assign something to what .upper() returns:
sntc = input("Please enter a sentence.")
sntc = sntc.upper()
or the shortened form:
sntc = input("Please enter a sentence.").upper()

Related

When I add a number "a" into a set, it return with an error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm writing a python code that I can add many numbers into a set and then print it. I don't know why does it return an error. This is my code:
ask = int(input('How many numbers you want to add in a set?'))
sett = set()
while ask:
a = int(input('Enter the number:')
sett.add(a)
ask = ask-1
if ask == 0:
print(sett)
break
But it returned this error:
‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎   File "Untitled5.py", line 5
    sett.add(a)
    ^
SyntaxError: invalid syntax
So anyone can find why is it wrong? This question may be easy to answer but please answer.
you forgot to close a parenthesis:
ask = int(input('How many numbers you want to add in a set?'))
sett = set()
while ask:
a = int(input('Enter the number:'))
sett.add(a)
ask = ask-1
if ask == 0:
print(sett)
break

Python .upper and .lower methods returning no results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm having trouble getting Python's .upper and .lower methods to return anything. Here's the code:
initials = input("Enter your initials: ")
uppercase = initials.upper
print(uppercase)
What it returns is:
Enter your initials: mj
<built-in method upper of str object at 0x7f50734b01f0>
I originally integrated this into a larger function, but when I call uppercase later in the function the variable remains empty. I'm working in Google Colab.
To call a method in Python, you must open and close parenthesis after the name of the method:
uppercase = initials.upper()
See the example in the documentations of str.upper.
Here you go, just make sure you use .upper() method appropriately with your variable. :)
initials = input("Enter your initials: ")
print(initials.upper())

I use re.sub in python3 and set count to 1,but it replaces all targets? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This my code:
import os,re
textFile = open('D:\\hehe.txt')
textContent = textFile.read()
print(textContent)
adjRegex = re.compile(r'ADJECTIVE')
nounRegex = re.compile(r'NOUN')
verbRegex = re.compile(r'VERB')
for i in range(len(textContent)):
if(adjRegex.search(textContent) != None):
print('Enter a adjective')
textContent = adjRegex.sub(input(),textContent)
elif(nounRegex.search(textContent) != None):
print('Enter a noun')
textContent = nounRegex.sub(input(),textContent)
elif(verbRegex.search(textContent) != None):
print('Enter a verb')
textContent = verbRegex.sub(input(),textContent,count=0)
else:
break
print(textContent)
textFile.close()
If this is your real code (i. e. you aren't actually using any complex regular expressions, just string matching), you could use simple str.replace:
adjRegex = "ADJECTIVE"
textContent = textContent.replace(adjRegex, input(), 1)
Note that then all the regexes will be mere strings.
And instead of searching for regexes, you could try checking whether the substrings are present in textContent:
if adjRegex in textContent:
# replace it!
You are using re.sub the wrong way.
Why are you iterating over every character in your text? You can
just go line by line using readlines() then replace()
Put the prompt message inside input('Enter a noun') instead of
printing it
Assign input to a variable to increase readability

Python - input from user [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
def _main_(userInput1):
if userInput1 ==create:
userName1=input('Please enter your username : ')
password1=input('Please enter your password : ')
createAccount(userName1, password1)
else:
userName2=input('Please enter your username : ')
password2=input('Please enter your password : ')
logIn(userName2,password2)
userInput1=input('Would you like to create a account or long in, type create or login ')
_main_(userInput1)
I keep getting an error with:
'create' is not defined
Can someone help please?
If you are checking whether userInput1 is equal to the string create, you need to put create in quotes, like this: 'create', or this "create". Otherwise, Python checks if userInput1 is equal to the variable create.
You need to change if userInput1 == create to if userInput1 == "create" as when you asked the user if they wanted to create a user, the input is by default a string and so you need quotes to tell the compiler to recognize it as a string rather than it seeing create as a variable.

Why does this script close immediately even though I ask for input? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I can't get the output in python with this code.
import random
die1=random.randrange(5)
die2=random.randrange(5)
total=die1+die2
input=("\nPress the ENTER key to exit.")
The black window immediately closes when opened
You are not calling input(); you are assigning a string to the name input instead.
Remove the =:
input("\nPress the ENTER key to exit.")
Over here, you are assigning input to a tuple, ("\nPress the ENTER key to exit.").
Instead, move the equals sign to before the input:
inp = input("\nPress the ENTER key to exit.")
Here is your edited code:
import random
die1=random.randrange(5)
die2=random.randrange(5)
total=die1+die2
inp = input("\nPress the ENTER key to exit.")
Now your input will be stored in the variable inp.

Categories