#Python help
231 messages · Page 1 of 1 (latest)
does it result in an error right now with those commas in first IF statement?
nvm, it works
first line, where you ask for input
even if there arent any vowels
get rid of that str() method, and put the prompt directly to input()
aight
did it work?
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
replace the commas with or's
and it should work now, because now you are checking if a is in x OR ... in x
aight
yeah, like that boolean operator
Nice!
ur gad damn genius robert
kinda
now onto a next thing
im gonna try to take a sentance or word
and print out the number of vowels in it
nvm, make a for loop, iterate through all of the letters and check if the letter is a vowel
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
okay, but you want to know how many vowels there are right?
yes
not the length of the word
not the total length
have you learned about for loops?
yes
then you should know that you can iterate through letters in strings?
so check each letter if it has vowel?
yes and no?
kinda yes
good!
idk how to ig convert it into code then
start with iterating through all of the letters in the string
aka the for loop part
try to get it working first
tell me if you are ready and you got that part working
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")
right now you are iterating through indexes of that string, but we can solve this task this way as well
have you studied how to access strings by indexes?
ye kind of
aka "somesting"[0] would be 's' then yeah?
basically in the first if block, instead of vowel in x, you do vowel in x[index]
so
'a' in x[index]
?
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
so do i add index after every x in that line right
yeah
aight one sec
and change the and's to or's because the letter can't be all of those vowels at the same time
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
yeah,
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
by index, I meant your variable i in that for loop
change the i to index and it should work fine
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?
yeah, but your if statement is a bit faulty
go over it once more and try to find what's wrong
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]):
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?
yeah
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
how im confused
hmm
did you get a different output?
add print(x[index]) to your for loop
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?
is there count function?
you don't need it
okk
a more simpler solution
ummm
|||make a variable which keeps track of it|||
try to think how would the loop affect our variable
would it perhaps reset it every iteration or would it not?
no
super confusion
initalize the variable before the for loop
and in that if block add 1 to that variable
uhh what
how else would you increment your counter variable c?
i am dum idk
hmmmm
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
oh
so now you got it why we need to add 1 to the counter variable?
c=0
for index in range (len(x)):```
wait
print("error")
c=c+1
print(x[index])```
like this?
yeah, but when it detects a vowel
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
it doesn't matter
so the code should work now?
so, now when you run this code and print out the counter after the loop, you should be getting an answer
did you add print(c) after your loop?
I think so, if it's outside of your for loop then it should be right yea
congrats! you've got a vowel counter!
a what
😭
welp its 2 am for me
i should head to bed
and try with a fresh mind tmmr
clearly it aint workin
you just have so much more other print statements, if you remove them then it's only that number
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
Nice!
MY GOD
good job!
I could have given you the answer right away, but that way you wouldn't have learnt much
true
you are a good teacher
- learning is actually through failing
indeed
the more you fail the better you will get, I did fail a lot too when I started out
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
anyway, I see it's rather late for you so better get some rest and keep coding tomorrow
gn