Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping help
#11
(Jan-15-2018, 03:19 PM)buran Wrote: Gribouillis already showed one way, using globals(). Other option is using vars(). If we go OOP, there are other options too
In any case I doubt OP restrictions on using dict, more over they don't provide the full code. Also, that is why we usually request to post the full homework assignment.
Thanks. You didn't say what was bad about it for a newbie though. (I appreciate eval/exec can be dangerous if used with input from a user of course.)

I thought the basic object referencing nature of Python was fundamental.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#12
A good reflex to teach is: Repetition of code? Write a function!
Reply
#13
(Jan-15-2018, 05:16 PM)Gribouillis Wrote: A good reflex to teach is: Repetition of code? Write a function!

I knew that and do, I just didn't know how to do so for this specific scenario.
Reply
#14
(Jan-15-2018, 01:45 PM)buran Wrote: It is really, really bad idea to teach this to newbie students...

@buran hello, I would like to ask why it is such a bad idea to teach newbies this? Is there some danger? Or is it just 'too complicated'? Thanks!
Reply
#15
bottom line - because it teaches bad coding practices.
As Gribouillis said, repeating code, means you need functions. They also showed relatively complex, but better (in my opinion) than using eval approach
the way you want to name variables - day1, day2... etc is wrong, wrong, wrong! that's what data structures like dict, list, etc. are used for. What will you do if you have data for one year (365 days), or 10 years, or 50 yesrs...? will you create 3650 or 18250 variables? In this particular assignment it may work with several variables, in real - life - no. I doubt you are really restricted to using dict...
Showing a newbie how to do it, especially with eval, given that a newbie is not fully aware of possible side-effects... I would compare with giving a loaded gun in the hands of a toddler. Once you have data in per lists, I can imagine asking user for input to retrieve data back. And using eval will expose you to risk of running a user-supplied code you don't actually want to run. Yes, in this particular case, user will not supply malicious code. But again - it's a matter of teaching bad coding.
Reply
#16
(Jan-16-2018, 04:49 PM)buran Wrote: bottom line - because it teaches bad coding practices.
As Gribouillis said, repeating code, means you need functions. They also showed relatively complex, but better (in my opinion) than using eval approach
the way you want to name variables - day1, day2... etc is wrong, wrong, wrong! that's what data structures like dict, list, etc. are used for. What will you do if you have data for one year (365 days), or 10 years, or 50 yesrs...? will you create 3650 or 18250 variables? In this particular assignment it may work with several variables, in real - life - no. I doubt you are really restricted to using dict...
Showing a newbie how to do it, especially with eval, given that a newbie is not fully aware of possible side-effects... I would compare with giving a loaded gun in the hands of a toddler. Once you have data in per lists, I can imagine asking user for input to retrieve data back. And using eval will expose you to risk of running a user-supplied code you don't actually want to run. Yes, in this particular case, user will not supply malicious code. But again - it's a matter of teaching bad coding.

I get your point and try to use those first, but as I said I cannot use dictionaries or 2 dimensional lists.
Reply
#17
Well, what CAN you use? Referencing globals, or using eval, or pretty much everything else mentioned in this thread, is extremely bad practice, and should never be used in real life. So I highly doubt your assignment is teaching you to do something you'd never want to use.
Reply
#18
Exactly my point from the start. Moreover globals(), vars() also use dict - the internal __dict__.
Reply
#19
(Jan-16-2018, 05:20 PM)nilamo Wrote: Well, what CAN you use? Referencing globals, or using eval, or pretty much everything else mentioned in this thread, is extremely bad practice, and should never be used in real life. So I highly doubt your assignment is teaching you to do something you'd never want to use.

I admire your optimism regarding the quality of assignments. Far too many Python assignments seem to be given out by people more familiar with other programming languages than Python.

I take the point on eval and global variables being bad practice, but allow that I am relatively new to Python myself and also I was trying to work within the constraints as described.

The example using globals() still doesn't make sense to me, so I need to explore. If global variable are bad practice though, why was this approach greenlit whereas eval was not (even though it wasn't using external input)?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#20
(Jan-17-2018, 10:02 AM)gruntfutuk Wrote: If global variable are bad practice though, why was this approach greenlit whereas eval was not
In this case, the correct approach is clearly to use a two dimensional list or a dict, but the OP says this correct approach is not allowed, so it remains only the bad solutions!

I don't think global variables are bad practice when they are read-only. It also happens that very experienced programmers use global statements so your mileage may vary. The bad thing is when beginners clutter their code with global statements.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020