#Python help

231 messages · Page 1 of 1 (latest)

next vessel
#
if ('a' in x,'e' in x,'i' in x,'o' in x,'u' in x):
   print("there are vowels")
else:
    print("no vowel is there")```
#

how do i print the vowels thing

devout arrow
#

does it result in an error right now with those commas in first IF statement?

#

nvm, it works

next vessel
#

no not an error

#

it just says there are vowels everytime

devout arrow
#

first line, where you ask for input

next vessel
#

even if there arent any vowels

devout arrow
#

get rid of that str() method, and put the prompt directly to input()

next vessel
#

aight

devout arrow
#

did it work?

next vessel
#

nope

#

still the same

#

thing

#
if ('a' in x,'e' in x,'i' in x,'o' in x,'u' in x):
   print("there are vowels")
else:
    print("no vowel is there")```
#

sorry if this is annoying this is my first time learning python

devout arrow
#

replace the commas with or's

#

and it should work now, because now you are checking if a is in x OR ... in x

next vessel
#

aight

next vessel
#

or?

devout arrow
#

yeah, like that boolean operator

next vessel
#

it works

devout arrow
#

Nice!

next vessel
#

ur gad damn genius robert

devout arrow
#

the syntax is important

#

you mess up one thing and boom! nothing's working

next vessel
#

💀

#

fr

devout arrow
#

kinda

next vessel
#

now onto a next thing

#

im gonna try to take a sentance or word

#

and print out the number of vowels in it

devout arrow
#

nvm, make a for loop, iterate through all of the letters and check if the letter is a vowel

next vessel
#
if ('a' in x or 'e' in x or 'i' in x or'o' in x or 'u' in x):
   print(len(x))
else:
    print("no integers")
#

its now writting the number of letters

#

if it has a vowel

devout arrow
#

okay, but you want to know how many vowels there are right?

next vessel
#

yes

devout arrow
#

not the length of the word

next vessel
#

not the total length

devout arrow
#

have you learned about for loops?

next vessel
#

yes

devout arrow
#

then you should know that you can iterate through letters in strings?

next vessel
#

so check each letter if it has vowel?

next vessel
#

kinda yes

devout arrow
#

good!

next vessel
#

idk how to ig convert it into code then

devout arrow
#

start with iterating through all of the letters in the string

#

aka the for loop part

#

try to get it working first

next vessel
#

hmmmm

#

aight

devout arrow
#

tell me if you are ready and you got that part working

next vessel
#

i got smth

#

its wrong

#

but something

#
for i in range (len(x)):
    if ('a' in x and 'i' in x and 'e' in x):
        print("number of vowels",(x))
    else:
        print("error")
devout arrow
#

right now you are iterating through indexes of that string, but we can solve this task this way as well

next vessel
#

ok

#

im all ears

devout arrow
#

have you studied how to access strings by indexes?

next vessel
#

ye kind of

devout arrow
#

aka "somesting"[0] would be 's' then yeah?

next vessel
#

oh yes

#

yes

#

ik

devout arrow
#

basically in the first if block, instead of vowel in x, you do vowel in x[index]

next vessel
#

so
'a' in x[index]
?

devout arrow
#

because, without that index you are compearing if the whole string has vowels in it, and that will result True every time the loop runs

next vessel
#

so do i add index after every x in that line right

devout arrow
#

yeah

next vessel
#

aight one sec

devout arrow
#

and change the and's to or's because the letter can't be all of those vowels at the same time

next vessel
#
for i in range (len(x)):
    if ('a' in x[index] or 'i' in x[index] or 'e' in x[index] or x[index]):
        print("number of vowels",(x))
    else:
        print("error")
#

this?

#

im a try to run it

devout arrow
#

yeah,

next vessel
#

Traceback (most recent call last):
File "C:/Users/picky/AppData/Local/Programs/Python/Python311/sdfsdfs.py", line 3, in <module>
if ('a' in x[index] or 'i' in x[index] or 'e' in x[index] or x[index]):
NameError: name 'index' is not defined

#

got an error

devout arrow
next vessel
#

ohhh

#

my bad

devout arrow
#

change the i to index and it should work fine

next vessel
#

the i in range right?

#

to index?

devout arrow
#

yeah

#

that way it will be easier

next vessel
#

didnt work

#
for index in range (len(x)):
    if ('a' in x[index] or 'i' in x[index] or 'e' in x[index] or x[index]):
        print("number of vowels",(x))
    else:
        print("error")```
#

this?

devout arrow
#

yeah, but your if statement is a bit faulty

#

go over it once more and try to find what's wrong

next vessel
#

uhhhh

#

idk 😭

devout arrow
#

the vowels are : a, e, i, o, u, first of all you are missing 2, second of all your last statement is always true because of how python considers string to be always True

#

and hence why it's not workign as have expected

#

so :
if ('a' in x[index] or 'i' in x[index] or 'e' in x[index] or 'o' in x[index] or 'u' in x[index]):

next vessel
#
for index in range (len(x)):
    if('a' in x[index] or 'i' in x[index] or 'e' in x[index] or 'o' in x[index] or 'u' in x[index]):
        print("number of vowels",(x))
    else:
        print("error")```
#

this?

devout arrow
#

yeah

next vessel
#

its just doin the same

#
error
number of vowels forty
error
error
error

output

devout arrow
#

no

#

before, it was all number of vowels forty, now it's that only when the current letter is a vowel

#

print out the vowel next to that message and you will see

next vessel
#

how im confused

devout arrow
#

x[index] is representing one letter of the string

#

so you print that out

next vessel
#

hmm

devout arrow
#

did you get a different output?

next vessel
#

i dont know wdym like print one leeter of the string

#

letter*

devout arrow
#

add print(x[index]) to your for loop

next vessel
#

oh ok

#

one sec

#
error
l
number of vowels lord
o
error
r
error
d
#

what

#
for index in range (len(x)):
    if('a' in x[index] or 'i' in x[index] or 'e' in x[index] or 'o' in x[index] or 'u' in x[index]):
        print("number of vowels",(x))
    else:
        print("error")
    print(x[index])
#

this?

devout arrow
#

yeah

#

do you understand why it is printing out different lone letters now?

next vessel
#

ye

#

its writting number of vowels only over where there is vowel

devout arrow
#

Good!

#

so, how would you count the occurrences of vowels?

#

@next vessel

next vessel
#

ummm

#

count how many times its detecting vowels?

devout arrow
#

yes!

#

and how will you keep track of vowels?

next vessel
#

is there count function?

devout arrow
#

you don't need it

next vessel
#

okk

devout arrow
#

a more simpler solution

next vessel
#

ummm

devout arrow
#

|||make a variable which keeps track of it|||

next vessel
#

oh

#

so outside the loop?

devout arrow
#

try to think how would the loop affect our variable

#

would it perhaps reset it every iteration or would it not?

next vessel
#

it would

#

oh wait

#

no it woudnt

#

it would keep the value right?

devout arrow
#

no

next vessel
#

super confusion

devout arrow
#

you made the variable by now?

#

and set it 0 right?

next vessel
#

not yet

#

one sec

#
    c=0
    print(x[index])```
#

?

devout arrow
#

initalize the variable before the for loop

next vessel
#

aight

#
c=0
for index in range (len(x)):```
#

like this?

devout arrow
#

and in that if block add 1 to that variable

next vessel
devout arrow
#

how else would you increment your counter variable c?

next vessel
#

i am dum idk

devout arrow
#

.

#

the C is your counter variable

next vessel
#

hmmmm

devout arrow
#

initally it's 0, because before the first loop we haven't gone through any of the letters yet, once we will go through them and check if the letter is a vowel we add 1 to our counter variable, because that is now the number of vowels we have in that string so far

next vessel
#

oh

devout arrow
#

so now you got it why we need to add 1 to the counter variable?

next vessel
#
c=0
for index in range (len(x)):```
#

wait

#
        print("error")
    c=c+1
    print(x[index])```
#

like this?

devout arrow
#

yeah, but when it detects a vowel

next vessel
#

oh ok

#
c=0
for index in range (len(x)):
    if('a' in x[index] or 'i' in x[index] or 'e' in x[index] or 'o' in x[index] or 'u' in x[index]):
        c=c+1
        print("number of vowels",(x))
    else:
        print("error")
    print(x[index])
#

?

#

or after print command

devout arrow
#

it doesn't matter

next vessel
#

so the code should work now?

devout arrow
#

so, now when you run this code and print out the counter after the loop, you should be getting an answer

next vessel
#
error
l
number of vowels lord
o
error
r
error
d
#

uhh

#

what

devout arrow
#

did you add print(c) after your loop?

next vessel
#

where

#
    print(x[index])
    print(c)
#

like this?

devout arrow
#

after your loop

#

not like this, the indention is wrong

next vessel
#
        print("error")
    print(x[index])
print(c)```
#

like this?

devout arrow
#

I think so, if it's outside of your for loop then it should be right yea

next vessel
#
error
l
number of vowels lord
o
error
r
error
d
1
#

shit

devout arrow
#

congrats! you've got a vowel counter!

next vessel
#

a what

#

😭

#

welp its 2 am for me

#

i should head to bed

#

and try with a fresh mind tmmr

#

clearly it aint workin

devout arrow
#

how's it not

#

the 1 is there, at the end

next vessel
#

yes

#

idk why its printing

#

wait

devout arrow
#

you just have so much more other print statements, if you remove them then it's only that number

next vessel
#
c=0
for index in range (len(x)):
    if('a' in x[index] or 'i' in x[index] or 'e' in x[index] or 'o' in x[index] or 'u' in x[index]):
        c=c+1
print(c)
#

IT WORKS

#

LETS GOOO

devout arrow
#

Nice!

next vessel
#

MY GOD

devout arrow
#

good job!

next vessel
#

WE DID IT ROBERT

#

can i add you robert

devout arrow
#

I could have given you the answer right away, but that way you wouldn't have learnt much

devout arrow
#
  • learning is actually through failing
next vessel
#

indeed

devout arrow
#

the more you fail the better you will get, I did fail a lot too when I started out

next vessel
#

ooo i see

#

welll robert this has been a wonderful experience

#

you taught me so much

#

thanks my guy

#

im a head to bed now

devout arrow
#

anyway, I see it's rather late for you so better get some rest and keep coding tomorrow

devout arrow