# Problem with wheat exercise - Grains.py

Hi!

I have problem with wheat exercise. I just don’t know where is the mistake, because code looks right. Maybe I just don’t understand the task. Please help me, here is my code:

def square(number):
if (number is int and number>=1 and number<=64):
return 2**(number-1)
else:
raise ValueError(“square must be between 1 and 64”)

def total():
sum = 0
for i in range(1,64):
sum = sum+2**(i-1)
return sum

You haven’t told us what error message is being reported.

In case it helps, here is some information about
range()
range()

@bkoro
so, I am a practically a total beginner (~3wk experience messing with python) so take anything I say with a grain of salt.

I didn’t know where to start with this exercise. I knew there was an exponential relationship but I didn’t know how to formulate the equation. Luckily, I found this post which helped me get started.

As far as I can tell, your square(number) function is correct.

In regards to the the total() function, I had been mulling over it for many hours trying to use what @keiraville posted. After a lot of trial and error, I figured out a possible solution using a while-loop with a boolean expression instead of a for-loop.

https://pythontutor.com/
above is a link to a tool that helps you visualize your code step by step. I used it to figure out said “solution.”

I put “solution” in quotations because I am still getting an error even though my code spills out the right answer every time.

Also, the total() function should have an argument passed to it like number is to the square() function. This is where I am getting an error that says total() should have a positional argument even though it is there in my code.

I purposefully didn’t post my code in case you wish to figure it out on your own. let me know if you’d like to see what I did.

If your solution attempt has

``````def total(arg):
...
``````

and a test calls

``````total()
``````

then Python will report an error in the test, because Python deduced from your definition of `total()` that an argument should be passed in. Actually, the test is fine, the bug is that `total()` should have been defined not to accept an argument. We should have

``````def total():
...
``````
1 Like

I see, then perhaps I don’t have a solution… I’ll keep trying.

Am I on the right track to be using a while-loop instead of a for-loop? Or are there multiple solutions with both types of loops?

In general, you can make a while loop work anywhere you find a for loop. However, for loops are generally simple and cleaner. If you’re going to use a loop here, a for loop would be a better choice.

1 Like

I did learn that but forgot. Thanks for the reminder.

Only test 8,9,and 10 passed. I fixed range function but it still doesn’t work :(
This is what i am getting:
ValueError: square must be between 1 and 64,
instead I should get 1 for test 1, and 2 for test 2

ValueError: square must be between 1 and 64 – am getting for every test except test 11, where message is: AssertionError: 9.223372036854776e+18 != 18446744073709551615

So the square function always raises an exception and never returns a number?

I don’t think this does what you think it does. You may want to entirely drop this part.