Python Forum
Homework, functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Homework, functions (/thread-22138.html)



Homework, functions - janoshz - Oct-31-2019

Hello,

I have this assignment and it kinda works on the platform. What is the problem, the character input. If the input is more than one character, the code still prints that out. For the assignment, it needs to be only one character.

Example output:

Give the text: hippo
Give the character: x
xxxxxxx
xhippox
xxxxxxx

This works. Next one is the problem:

Give the text: sausage
Give the character: **
Invalid input! Type only one character!
Give the character: *
******************
**sausage**
******************

So the code does not register the new input. I kinda had a too long break between my assignments, so I have forgotten something. I'd be very happy for any assistance you might have for me, thanks :)

def lenght(x):
    
    while len(x) != 1:
        print("Invalid input! Type only one character!")
        cha = str(input("Give the character: "))
        break

def decoration(y, x):
    
    decorated = y * (len(x) + 2) + "\n" #top row
    decorated = decorated + y + x + y + "\n" #middle row
    decorated = decorated + y * (len(x) + 2) + "\n" #bottom row
    return decorated

def main():
    
    tex = str(input("Give the text: "))
    cha = str(input("Give the character: "))
    
    lenght(cha)
    decoration(cha,tex)
    
    plaque = decoration(cha, tex)
    print(plaque)

main()



RE: Homework, functions - ichabod801 - Oct-31-2019

you need to return cha from lenght and assign it in main, just like you do with returning decorated from decoration and assigning it to plaque.


RE: Homework, functions - janoshz - Oct-31-2019

Yeah, thanks. Missed the return cha... buuut, I don't get it still. Just missing something again. Thanks though!


RE: Homework, functions - nilamo - Oct-31-2019

Are you getting an error? What's the error?
Or is the output different from what you expected? What did you get vs what did you expect?


RE: Homework, functions - ichabod801 - Oct-31-2019

And please show us your current code, in case you missed something.


RE: Homework, functions - janoshz - Oct-31-2019

Same code as above, just added the "return cha" to the def lenght. No errors, but we are using a platform for the code which wants a certain outcome. Before the code are the variations I get, the first is ok as far as the platform is considered. Second one prints just the first input of two characters where there should be only one and the platform does not accept it since it prints out wrong output. I've just been staring at this too long, maybe I'll get it tomorrow :D


RE: Homework, functions - nilamo - Oct-31-2019

If that's the only change you made, then 1) you're not using the return value, so it doesn't matter if you called the function, and 2) the function itself has a while loop testing something different than you're returning, and could be returning garbage untested data or creating an infinite loop, depending on exactly where you put the return statement.


RE: Homework, functions - janoshz - Nov-02-2019

Yeah, I just can't call the cha to be the cha. Dance I know, cha cha cha, but hell's. :)

Edit: I changed while to if, it works as should, but still I need to get the second input.


RE: Homework, functions - ichabod801 - Nov-02-2019

Dude, cha = leght(cha).