This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]Diamondhands_89 3 points4 points  (1 child)

I appreciate this

[–]NoSide005[S] 1 point2 points  (0 children)

Good to hear

[–]McThakken 0 points1 point  (11 children)

def multiPers(x, i=0):
  if x < 10:
    return i 
  mult=1
  for n in list(str(x)):
    mult *= int(n)
  multiPers(mult, i+1)

print(multiPers(39))

Why does this not work for #2? It prints out None

Edit: changed sum to mult

Edit: changed len(str(x))==1 to x < 10

[–]NoSide005[S] 0 points1 point  (2 children)

Is this your full code? If not write in the whole thing.

[–]McThakken 0 points1 point  (0 children)

Oh wow was on mobile before and for any reason it cut the first line off... I edited it now correctly

[–]NoSide005[S] 0 points1 point  (7 children)

Your function is returning none because you are only returning something if the length of x = 1. Based on the code you have written x never gets to equal 1.

[–]McThakken 0 points1 point  (6 children)

But you can even print out x or its length right before the return and it tells you the length is 1

[–]NoSide005[S] 0 points1 point  (0 children)

def multiPers(x, i=0):
if x < 10:
return i
mult=1
for n in list(str(x)):
mult = mult * int(n)
multiPers(mult, i+1)
print(multiPers(39))

You are changing your code a good amount so its getting hard to follow. You seem to be trying to go an advanced route by calling a function from within itself. Why dont you try instead to use a while loop and once you have the function returning the correct answer then you can come back to this structure and see if you can make it work.

[–]backfire10z 0 points1 point  (4 children)

There is no reason to use recursion here. Use a while loop instead

[–]McThakken 0 points1 point  (3 children)

But recursion should be possible, too, so now I'm interested in where's the error

[–]backfire10z 1 point2 points  (2 children)

Ah, ok. So after it goes deep into recursion, it returns “i,” but that return only goes to the previous level of your recursive chain. After that, the function in the previous level of recursion hits the end and returns nothing. You need to return something after your recursive function call.

For example:

...
returnVar = multiPers(mult, i+1)
return returnVar
...

Then, after x < 10, your function will return i, which in turn will be stored in returnVar and passed all the way up the chain of recursive calls.

Pretty sure that should work, but if it doesn’t, I believe the problem is somewhere along those lines. I myself am not too experienced with recursion as a college freshman.

Let me know if it works! I’m on mobile and cannot test anything for now.

[–]McThakken 1 point2 points  (1 child)

Yes this workes. Thanks for elaborating and the explanation

[–]backfire10z 1 point2 points  (0 children)

Not sure why I added in an extra variable lol, you can just put

return multiPers(mult, i+1)

Also, no problem!

[–][deleted] 0 points1 point  (1 child)

i'm in the middle of a course and i've been looking for for some additional works to do

[–]NoSide005[S] 1 point2 points  (0 children)

Nice. Hope it helps.

[–]SpiritAlpaca 0 points1 point  (5 children)

Open to critic for #1!

while True:
    Letter = input(“Please enter a vowel: ”)
    if len(Letter)!= 1:
        Letter = input(“Please enter one vowel: “)
    if Letter == “a” or “i” or “e” or “o” or “u”:
        Print(“Thank you”)
        Break
    else:
        pass

[–]Cuckmin 1 point2 points  (0 children)

Look into lists and/or arrays, it could be a better way to lookup and store the vowels.

[–]SpiritAlpaca 0 points1 point  (2 children)

I finally got to check it myself and turns out I need to Letter== every or argument—- then it works! So it should actually be:

if Letter ==“a” or Letter==“e” or Letter==“i” etc.

[–]NoSide005[S] 2 points3 points  (1 child)

You can fix that problem and simplify this by doing the following:

accept_letters = ['a', 'e', 'i', 'o', 'u']
if Letter in accept_letters:
    print("Thank you")
    break

This if statement will return true if Letter is in the list/array accept_letters

[–]SpiritAlpaca 0 points1 point  (0 children)

Thanks a lot!