0
minutes = 0
def sec_to_min():
    seconds = int(input("How many seconds you want to convert?"))
    while True:
        if seconds >= 60:
            seconds =-60
            minutes =+1
            print(minutes)
            return True
        else:
            print(seconds,"sec")
            return False

sec_to_min()

When I input 60 or more, it will print just "1", nothing more. My if is not working. I tried to move "seconds" in front of function but it gave me an error, something like "local variable 'seconds' referenced before assignment"

6
  • 4
    Did you mean to write seconds -= 60 (decrement seconds by sixty) instead of seconds = -60 (setting seconds to negative sixty)? Same for minutes (that's why it only returns 1; you only ever set it to 1). Dec 1, 2016 at 18:03
  • 1
    Please format your code properly. The problem is that you return from the function in both if and else statements. And do you have to do that with the loop. Dec 1, 2016 at 18:04
  • thank you guys for advice, but i dont really know what do you think here, can you update my code please? Dec 1, 2016 at 18:06
  • The first comment has told you exactly what is wrong and how to fix it. Dec 1, 2016 at 18:09
  • 1
    @MatejPakán is quite correct; fixing the code is your job. Others found and described the problem.
    – Prune
    Dec 1, 2016 at 18:10

2 Answers 2

1

I'll summarize:

We think you've confused two types of statements:

seconds = -60
seconds -= 60

The first one deletes the previous value of seconds and assigns a new value of -60; the second is equivalent to

seconds = seconds - 60

The flow of your code suggests that this second one is what you need; the same change applies to minutes.

You have a conflict in your while logic: although the loop control seems to want to repeat the loop for a while, it gets interrupted during the first iteration by one return statement or the other. You have no way to reach the bottom of the loop and go back for a second iteration.

Why are you returning a Boolean value? That's not clear, since your main code doesn't use it. If you do require the return value, perhaps you need to set a variable within the loop, and then return that variable after the loop.

2
  • yes, that helped me a lot, also i needed to remove return True Dec 1, 2016 at 18:25
  • 2
    Also remember to up-vote things you found helpful, such as the seminal comments you got right away.
    – Prune
    Dec 1, 2016 at 18:59
0
def sec_to_min():
    minutes=0
    seconds = int(input("How many seconds you want to convert?"))  
    while True:    
        if seconds>=60:
            while seconds>=60:
                seconds =seconds-60
                minutes+=1
            print(minutes)
            break
        else:
            print(seconds,"sec")
            break
sec_to_min()

the problem was that you run the if only one time + the symbols were all upside down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.