PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 22, 2022

[FIXED] How to use multiple conditions in a for loop in a function?

 November 22, 2022     multiple-conditions, python     No comments   

Issue

I'm still new to python and have just started learning. The task given is to find the amount of punctuation, vowels, and constants in a given text. But whenever I run the code it just gives me a 0.

def getInfo(text):

    pun = [".", ",", " ", "\'", "\"", "!"]
    vowels = ["a", "e", "i", "o", "u"]

    count = 0
    count2 = 0
    count3 = 0
    for char in text:
        if char in pun:
           count += 1
        return count
    
        if char.lower() in vowels:
           count2 += 1
        return count2
        
        if (not char.lower() in vowels) and (not char.lower() in pun):      
            count3 += 1
        return count3

Solution

Return should be outside the loop.

It should be :

def getInfo(text):

    pun = [".", ",", " ", "\'", "\"", "!"]
    vowels = ["a", "e", "i", "o", "u"]

    count = 0
    count2 = 0
    count3 = 0
    for char in list(text):
        if char in pun:
           count += 1
   
        if char.lower() in vowels:
           count2 += 1
        
        if (not char.lower() in vowels) and (not char.lower() in pun):      
            count3 += 1
    return (count, count2, count3)

getInfo('myname.is hello world!')

# (4, 6, 12)


Answered By - Pygirl
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing