Python Forum

Full Version: New Python Student = Does this code look right?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If anyone is willing to chime in, I am brand new to Python (and all things computer programming). I'm taking an online course and this particular task has baffled me.

First it took me so long to even yield an answer/output that resembled something as possibly correct. Then I'm still doubting my answer/output because I don't know if I read the task correctly, if the task's instructions are written poorly so it's not entirely my fault, or if I'm just messing up altogether.

If anyone would be willing to tell me what they think, did I read the instructions wrong, is there a better way to do what I did (if I happened to be correct), etc., I'd really appreciate it.

Here are the task's instructions, my code, and the answer/output I got ("hi! HI! HEY YOU!"):

Define yell_this() and call with variable argument
-define variable words_to_yell as a string gathered from user input()
-Call yell_this() with words_to_yell as argument
-get user input() for the string words_to_yell

# [ ] define yell_this()
def yell_this(phrase = "Hey you"):
    

# [ ] get user input in variable words_to_yell
    words_to_yell = input().upper()
    print(words_to_yell + "! " + phrase.upper() + "!")
    

# [ ] call yell_this function with words_to_yell as argument
yell_this()
answer/output:
Output:
hi HI! HEY YOU!

Sincerely,
A super brand new student to programming
Hi musicjoeyoung,

The arguments of a function are the values/variables passed to that function (what you put between the parentheses), so I am interpreting "Call yell_this() with words_to_yell as argument" to mean your function call should look like:
yell_this(words_to_yell)
The instructions for the task are not as clear as they could be in my opinion. I think you need to:
-Define yell_this() to accept a variable as an argument
-Get a string from the user with input() and assign that string to a variable called words_to_yell
-Call the yell_this() function with the code shown above (using the variable words_to_yell as the argument)
Generally, I wouldn't use
def yell_this(phrase = "Hey you"):
Instead, I would use :
def yell_this(phrase):
However, you can do it any way you want - it's your choice. Then later, you can either define the phrase and call the function like :
phrase1 = "Hey you!"
yell_this(phrase 1)
Or you can also do :
yell_this("Hey You!")
Either of the 2 ways, output will be same
(May-06-2020, 08:42 AM)pyzyx3qwerty Wrote: [ -> ]Don't use
def yell_this(phrase = "Hey you"):


@pyzyx3qwerty,this is really poor advise. There is nothing wrong to have keyword parameter and default value of the argument if they see fit.
Now, if the homework assignment requires/allows for this is completely different matter.
(May-06-2020, 09:09 AM)buran Wrote: [ -> ]@pyzyx3qwerty,this is really poor advise. There is nothing wrong to have keyword parameter and default value of the argument if they see fit.
Now, if the homework assignment requires/allows for this is completely different matter.

@buran, well generally, I this is the first time that I have seen someone do like this. However, I sincerely apologize
for any inconvenience caused. And since he is asking for us to review this code, shouldn't this be in the Code Review section (I'm not sure) ?
(May-06-2020, 09:17 AM)pyzyx3qwerty Wrote: [ -> ]@buran, well generally, I this is the first time that I have seen someone do like this. However, I sincerely apologize
for any inconvenience caused. And since he is asking for us to review this code, shouldn't this be in the Code Review section (I'm not sure) ?
no problem, I simply didn't want to give OP wrong impression that this is incorrect or wrong practice. The problem with their code is elsewhere - it doesn't conform to the assignment. Of course, given that assignment doesn't explicitly ask for default argument value, it's possible to pass it as positional parameter instead of keyword one.
If it should be in Code Review - no, it's a homework assignment (this takes precedent) and also it really doesn't conform to their assignment anyway - i.e. it's not that they ask for review/advise how to improve it no mater how they frame it.
Thanks everyone!