Jump to content

Hello,

 

I don't see what I did wrong with my code here, though obviously, I did.

If someone could point out where I went wrong, I'd appreciate it.

 

tempList = []
def average(listTemps):
    return float(sum(listTemps)) / max(len(listTemps), 1)
iterator = 0
def end():
    global iterator
    if tempList[iterator] < average(tempList):
        print str(tempList[iterator]) + " is less than the average."
        iterator += 1
        end()
    elif tempList[iterator] > average(tempList):
        print str(tempList[iterator]) + " is more than the average."
        iterator += 1
        end()
    else:
        print str(tempList[iterator]) + " is equal to the average."
        iterator += 1
        end()
counter = 1
def askMain():
    global counter
    temp = input("Please enter a temperature, " + str(counter) + " of 7: ")
    tempList.append(temp)
    if counter <= 7:
        askMain()
        global counter
        counter += 1
    else:
        end()
askMain()

 

Thanks in advance

-LtStaffel

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/717167-python-global-variable-problem/
Share on other sites

Link to post
Share on other sites

You see, you never add 1 to counter, because first what you do you run another askMain function, so you never reach that line where counter is increased, also you don't need that second global counter thing. That for counter, you will probably encounter more errors after you fix this one.

Link to post
Share on other sites

1 hour ago, LtStaffel said:

If someone could point out where I went wrong, I'd appreciate it.

1. You're using Python 2. Unless you have a really good reason, don't do that.

2. Don't use global variables if at all possible.

3. Your use of recursion is weird and unnecessary.

 

Here's your code rewritten in Python 3.6

Spoiler

#! /usr/bin/env python3.6


def main():
    temps = [float(input(f'Please enter a temperature, {i} of 7: ')) for i in range(8)]
    average = sum(temps) / len(temps)

    for t in temps:
        if t < average:
            print(f'{t} is less than the average.')
        elif t > average:
            print(f'{t} is more than the average.')
        else:
            print(f'{t} is equal to the average.')

if __name__ == '__main__':
    main()

 

 

1474412270.2748842

Link to post
Share on other sites

7 minutes ago, fizzlesticks said:

1. You're using Python 2. Unless you have a really good reason, don't do that.

2. Don't use global variables if at all possible.

3. Your use of recursion is weird and unnecessary.

 

Here's your code rewritten in Python 3.6

  Hide contents


#! /usr/bin/env python3.6


def main():
    temps = [float(input(f'Please enter a temperature, {i} of 7: ')) for i in range(8)]
    average = sum(temps) / len(temps)

    for t in temps:
        if t < average:
            print(f'{t} is less than the average.')
        elif t > average:
            print(f'{t} is more than the average.')
        else:
            print(f'{t} is equal to the average.')

if __name__ == '__main__':
    main()

 

 

I appreciate the re-write of my script, but using recursion over loops was part of the challenge in making this program.

What should I use instead to make this work?

I can use 3.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

1 hour ago, Mr_KoKa said:

You see, you never add 1 to counter, because first what you do you run another askMain function, so you never reach that line where counter is increased, also you don't need that second global counter thing. That for counter, you will probably encounter more errors after you fix this one.

I thought making the variable global would fix that.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

19 minutes ago, LtStaffel said:

using recursion over loops was part of the challenge in making this program.

Unfortunately I don't know how to help you there, recursion just doesn't make sense for this problem.

 

19 minutes ago, LtStaffel said:

I thought making the variable global would fix that.

The problem is that you're recursing before you increment the counter. So in every recursive call you'll have a value of 0 for counter, your if statement returns true, then you make another recursive call and eventually reach Python's very small recursive call limit. 

Moving the increment up before the recursive call will give counter the value 0 then 1 then 2 etc as expected.

1474412270.2748842

Link to post
Share on other sites

7 hours ago, Erik Sieghart said:

I'm guessing it was homework. There's no application for recursion outside of academia

Actually I'm learning Haskell and thought I could pull off this program in Python without loops after seeing how recursion worked in Haskell.

 

I am trying to learn new ways of going about things, even if recursion doesn't make sense in this particular case or most cases, I figure knowing how to do the same thing in more than one way can't hurt :)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

2 hours ago, Erik Sieghart said:

Recursion is something to know so that you know not to.

Not in a functional programming language unfortunately

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

11 hours ago, Erik Sieghart said:

I'm guessing it was homework. There's no application for recursion outside of academia

Recursion works really well when you need to traverse a tree or some other similar data structure.

 

Many would say if you use a "goto" statement in your code, you should have an anvil dropped on your head. Well, they can go ahead and mail one to Linus Torvalds, because there's at least one "goto" statement in the Linux kernel code.

Link to post
Share on other sites

def main():
    temps = [float(input(f'Please enter a temperature, {i} of 7: ')) for i in range(8)]
    average = sum(temps) / len(temps)

    for t in temps:
        if t < average:
            print(f'{t} is less than the average.')
        elif t > average:
            print(f'{t} is more than the average.')
        else:
            print(f'{t} is equal to the average.')

if __name__ == '__main__':
    main()


HEY!! An F-String. That's the first code example I've seen of one outside of PEP-498. I guess I should upgrade to 3.6, but my classes won't accept it.

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×