Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework, functions
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yeah, thanks. Missed the return cha... buuut, I don't get it still. Just missing something again. Thanks though!
Reply
#4
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?
Reply
#5
And please show us your current code, in case you missed something.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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
Reply
#7
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.
Reply
#8
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.
Reply
#9
Dude, cha = leght(cha).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python homework / functions sunhyunshine 1 2,475 May-11-2019, 05:37 PM
Last Post: MrTheOne

Forum Jump:

User Panel Messages

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