Python Forum
What the butt is MyProgramming lab asking me to do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What the butt is MyProgramming lab asking me to do?
#1
The problem states: "Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression's value is "freshman"; 30-59 would be a "sophomore", 60-89 would be "junior" and 90 or more would be a "senior"."

I see that it's asking for an expression, but I have no idea what it wants me to put. The code I've found that works when assigning a value to "credits" is:

credits = 3

if credits <= 30:
    print("freshman")

elif credits >= 89:
    print("senior")


elif credits >= 60:
    print("junior")

elif credits >= 30:
    print ("sophomore")
>>> freshman

But MyPLab states that this is wrong. What the butt does it want from me
Reply
#2
Start with highest value:
value = None
if credits > 90:
    value = "senior"
elif ...
print(value)
Reply
#3
Note that "freshman" is for LESS than 30, not 30 as well!

Incidentally, as Python lacks the switch/case construct of many other languages, it is common to use a dictionary instead:

class LevelDict(dict):
    def __getitem__(self, item):
        if type(item) != range:
            for key in self:
                if item in key:
                    return self[key]
        else:
            return super().__getitem__(item)

level_check = LevelDict({range(0, 30):  'freshman', 
                     range(30,60):  'sophomore',
                     range(60,90):  'junior',
                     range(90,999): 'senior' 
                    })
while True:
  creditstr = input('\nEnter credit level (return to end): ')
  if creditstr:
    level = level_check[int(creditstr)]
    print("Level achieved: {}".format(level))
  else:
    break
I am trying to help you, really, even if it doesn't always seem that way
Reply
#4
(Oct-01-2017, 08:49 AM)Larz60+ Wrote: Start with highest value:
value = None
if credits > 90:
    value = "senior"
elif ...
print(value)

The code works fine for any value I put in (except 30 of course, but that's fixed). This isn't the problem I am having, nor the question I asked
Reply
#5
Hum. "I see that it's asking for an expression, but I have no idea what it wants me to put."

Usually, in programming, an expression is anything on the right of the assignment (or utilised in a statement) that results in some value, be that numerical, a string, some data expression. The text "Write an expression whose value is ..." I understand to mean write f(x) where a one of the listed values is returned, be it in the form level = f(credits) or print(f(credits).

What is it you think we are missing?
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

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