Python Forum
Python uppercase conversion conditions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python uppercase conversion conditions
#1
I'm currently trying to do a program related to Python uppercase conversion conditions.
1. If the length of a word is less than 7, it is not converted.
2. If the word length is more than 7, I want to convert the first vowel to upper case.

I ran the code according to the conditions I set, and the nameerror popped up like this
NameError: name 'newword' is not defined
I've certainly defined newword, but why am I getting same error?

input example)
what a beautiful world!
output example)
what a bEautiful world!



def Upper_Vowels(newword):
    sentence = input()
    newword = sentence.split()
    for i in range(len(newword)):
        if i >= 7 :
            def capital(sentence,vowel):
                vowels = 'aeiou'
                newsentence = []
                for word in sentence:
                    if word.lower() in vowels:
                        word = word.isupper()
                newsentence.append(newword)
                return ('[]'.join(newsentence))
                
    
if "__main__" == __name__:
    sentence = input()
    print(Upper_Vowels(newword))
Reply
#2
Your error message should have told you where it happened as well. In this case, you've defined (or assigned) newword within the Upper_Vowels function. But it's not defined outside.

You've tried to use that variable on line 18 where it is not in scope. Presumably you wanted to pass sentence to the function, not newword.
Reply
#3
(Apr-29-2020, 07:01 AM)bowlofred Wrote: Your error message should have told you where it happened as well. In this case, you've defined (or assigned) newword within the Upper_Vowels function. But it's not defined outside.

You've tried to use that variable on line 18 where it is not in scope. Presumably you wanted to pass sentence to the function, not newword.


this is error message on line 18 where it happened

print(Upper_Vowels(newword))
NameError: name 'newword' is not defined


When I remove that character 'newword' from line 18,

print(Upper_Vowels())
then I got a typeerror on line 18, like this.
TypeError: Upper_Vowels() missing 1 required positional argument: 'newword'
Reply
#4
Right. You have to pass something.

But you just assigned sentence on the line before. I presume *that* is the variable you were intending to pass in.
Reply
#5
Is this code what you mean?

def Upper_Vowels(newword):
    sentence = input()
    for i in range(len(newword)):
        if i >= 5 :
            def capital(sentence,vowel):
                vowels = 'aeiou'
                newsentence = []
                for word in sentence:
                    if word.lower() in vowels:
                        word = word.isupper()
                newsentence.append(newword)
                return ('[]'.join(newsentence))
                
    
if "__main__" == __name__:
    sentence = input()
    newword = sentence.split()
    print(Upper_Vowels(sentence))

Reply
#6
What is line 17 doing for you? You assign something to a variable and then never use it. It could be removed.

The section starting at line 5 is creating a new function. While it is possible to define a function inside another, you probably want to avoid that. Either keep the code you need in Upper_Vowels, or put it as a completely separate function (not nested inside this one). The way you have it, the function "capital" is created, but never called.
Reply
#7
Try this:
first = input("Enter a sentence : ")
second = ""
vowels = ['a','e','i','o','u']
for i in first:
    if i in vowels:
        second += i.upper()
    else :
        second += i
print( second)
And output:
Output:
Enter a sentence : This is a test ThIs Is A tEst
Reply
#8
Your logic is kinda messed up.
Think of a function as a tool. Your main routine then uses that tool. To keep your tool flexible, you probably do not want to get input in that tool, rather to get input in the main routine (eliminate line 2, it is redundantly asking the user to repeatedly enter the sentence).

Then you conditionally create another tool, capital(), that you never use. Creating it does not use it. And, if len(newword) is say 10, you define that tool 5 times as the definition is inside a loop. Keep your function definitions outside of loops and outside of if statements.

As written, UpperVowels() does not return anything. Capital() does, but since it is never called the best you can hope for is nothing.

The parameters in a function definition are placeholders. If they have the same name as something elsewhere in the program then it is like two people with the same names. Same names, but different people. newword in the main routine and newword in your function are not the same.

Better to use unique variable names in your function to avoid this confusion. Remember the function is a tool, acting on parameters to give you a desired output.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 1,994 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  C to Python code conversion print problem anakk1n 1 378 May-22-2020, 04:15 PM
Last Post: deanhystad
  MATLAB to Python conversion stokd 10 1,181 Jan-19-2020, 09:14 PM
Last Post: stokd
  simple if elif conditions in Python Chandan 2 451 Jan-08-2020, 02:36 PM
Last Post: DeaD_EyE
  Excel Model Conversion into Python line7 2 516 Oct-09-2019, 07:51 AM
Last Post: line7
  macron on top of uppercase letters newbieAuggie2019 3 826 Aug-21-2019, 08:06 PM
Last Post: newbieAuggie2019
  python opencv grayscale conversion error Spandora 1 5,592 May-26-2019, 10:43 AM
Last Post: heiner55
  Vba conversion to Python stranger14u 1 1,050 May-26-2019, 08:01 AM
Last Post: heiner55
  'True' if 'string' has at least one uppercase letter ClassicalSoul 1 736 Feb-28-2019, 12:08 PM
Last Post: buran
  Conversion of Time Duration in seconds in python jdevansh99 0 1,089 Jun-05-2018, 05:12 PM
Last Post: jdevansh99

Forum Jump:

User Panel Messages

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