Python Forum
New Python Student = Does this code look right?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New Python Student = Does this code look right?
#1
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
Reply
#2
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)
Reply
#3
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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) ?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#6
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks everyone!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Student project - alert action when X happens Y amt of times, how? unknown00 2 1,796 Aug-25-2021, 08:07 PM
Last Post: namarang
  Student grader and sorter for assignment RazeAD 7 3,173 Feb-11-2021, 06:29 AM
Last Post: RazeAD
  Generating a student's transcript [OOP concept] aongkeko 2 2,689 Dec-01-2020, 06:43 AM
Last Post: buran
  Create code for input names and score for 15 student Davin 2 2,066 Sep-21-2020, 08:49 AM
Last Post: DeaD_EyE
  Student grade program help debug ccm1776 3 5,144 Nov-14-2018, 02:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

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