-1

I'm a high highschool student taking an online course for python, and one of the assignments is to create a function similar to fizz buzz, except instead of "fizz" and "buzz", they simply use "three" and "five". I wrote a function but unfortunately it completely failed, and doesn't do its job at all. I've been having a hard time to figure out what to input instead of "for x in range(101):", I think that's where my problem is. Any pointers/guidance would be very helpful, I'm completely lost.

The instructions are here and here is my code:

def student_func(x):
    for x in range(101):
        if x % 3 == 0 and x % 5 == 0:
            return('threefive')
        elif x % 3 == 0:
            return('three')
        elif x % 5 == 0:
            return('five')
        else:
            return x

Edit:A couple of people were recommending me to delete the loop, since it was starting off the function with 0(I should've realized that). However when I took it out the output became None. Am I doing something else wrong, or am I misunderstanding? Thanks for the speedy answers however, I'm still very inexperienced

Code with new error:

def student_func(x):
    if x % 3 == 0 and x % 5 == 0:
        return('threefive')
    elif x % 3 == 0:
        return('three')
    elif x % 5 == 0:
        return('five')
    else:
        print(x)
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73

4 Answers4

0

So, a few things to note. First, the range(n) function returns an iterator that will go through 0, 1, 2, ..., n-1. Thus, the first value you will iterate over in your code will be 0. Think about how your for loop will handle that.

Second, because you're assigning x to be the values iterated over from the range function, you're never actually testing the x being passed into the function.

Third, note that every branch of the if-elif-else tree inside the loop has a return statement, thus the loop is only going to go around once before returning a value. That sort of defeats the purpose of using a loop.

Lastly, make sure you are aware of how 0 % n evaluates. Think about things and see if you can reach a conclusion about why your program is returning student_func(1) = 'threefive'.

BowlingHawk95
  • 1,518
  • 10
  • 15
  • I removed the for loop, because I only want the function to run once. I think I got your first and last point, but I don't get the second one. – user9970478 Jun 21 '18 at 03:37
  • So, this function takes an argument `x` into it, and you're supposed to test if *that* `x` is divisible by 3 and/or 5. When you write `for x in ...`, you're going to be changing the value of `x` (in the case of the original code to be 0, 1, ..., n-1), which isn't what you want. If you change the value of `x`, then you lose the original `x` that you were trying to test. You've probably fixed this by removing the for-loop, but make sure you're aware that if you want to test the `x` you're given, then you shouldn't be modifying its value, and a for loop as written would modify it. – BowlingHawk95 Jun 23 '18 at 15:05
0

The issue is that the "tester" which tests your function gives the function the number/argument - x. However, what you are doing is testing the function from 0 - 100. The question does not ask you to test your function with numbers from 0 - 100. Therefore, you should remove the for loop.

Why is it giving the output "threefive"? Well, the function first tests in the loop the number 0. It satisfies the first if statement, and returns (so it gets out of the function).

RealPawPaw
  • 988
  • 5
  • 9
0

Right now your code is looping through every value from 0 to 100 and returning if the first value (0) is divisible by both three and five, which of course it is. The function is only asking you to check if a single value is divisible by three, or five, or both. Why would you need a loop?

Keveloper
  • 773
  • 5
  • 10
0

Your function isn't supposed to loop; it's supposed to examine only the single value passed in as the argument. The teacher's function will call your function with a variety of inputs.

Take out the for loop statement and it should work.

John Gordon
  • 29,573
  • 7
  • 33
  • 58