Home > Enterprise >  Having trouble, with the output of my code in my zyBooks python assignment, it is giving me extra sp
Having trouble, with the output of my code in my zyBooks python assignment, it is giving me extra sp

Time:03-09

I was wondering if anyone can tell me what I'm doing wrong? My output is suppose to be:

You entered: yellow Daisy 6

First password: yellow_Daisy Second password: 6yellow6

Number of characters in yellow_Daisy: 12 Number of characters in 6yellow6: 8

My output is:

You entered: yellow Daisy 6 First password: yellow _Daisy Second password: 6yellow 6

Number of characters in yellow _Daisy :14 Number of characters in 6yellow 6:9

My code:

fav_color = input()
fav_flower = input()
fav_numb = input()

print('You entered:', fav_color,fav_flower, fav_numb)
password1 = fav_color   '_'   fav_flower
password2 = fav_numb   fav_color   fav_numb

print('First password:', password1)
print('Second password:', password2)

print()
    
print(f'Number of characters in {password1}:{len(password1)}')  
print(f'Number of characters in {password2}:{len(password2)}')

CodePudding user response:

I'm unable to reproduce your error: have you messed with the end value? If not, either your code formatting on SO messed up or your IDE is weird. As for the spaces, you're typing them in yourself with the input, see below:

>>> x = input()
... y = input()
... print(x   y)

> Hello # I've entered a space after Hello
> Goodbye
Hello Goodbye # The space is preserved, it's not 'HelloGoodbye'

If you don't want the spaces, use .strip(), as shown here:

>>> x = input()
... y = input()
... print(x.strip()   y.strip())

> Hello # Still a space
> Goodbye
HelloGoodbye # No space!

If you don't want ANY spaces in the string, use .replace() as shown:

>>> x = input().replace(' ', '') # Replace all spaces with nothing
... y = input().replace(' ', '') # AKA delete all spaces
... print(x   y)

> h e l l o
> b y e
hellobye # No spaces here!

Other than this, your code executes perfectly fine.

EDIT: Your code with .strip():

fav_color = input().strip()
fav_flower = input().strip()
fav_numb = input().strip()

print('You entered:', fav_color, fav_flower, fav_numb)
password1 = fav_color   '_'   fav_flower
password2 = fav_numb   fav_color   fav_numb

print('First password:', password1)
print('Second password:', password2)

print()

print(f'Number of characters in {password1} is : {len(password1)}')
print(f'Number of characters in {password2} is : {len(password2)}')

You can execute/test it yourself.

The .strip() tutorial, The .replace() tutorial

As for wanting to print your first_password, second_password on the same line, refer @CryptoFool's answer, or the end keyword for python (This question).

CodePudding user response:

Each print() produces a new line. It prints what you ask it to and then prints a newline at the end to cause what you print next to print to a new line.

The most straightforward way to keep more on one line is to use fewer print() statements so that you retain the on-to-one between print() statements and lines. Here's how to fix up your code to do that and get about what you say you want:

print('You entered:', fav_color, fav_flower, fav_numb)

password1 = fav_color   '_'   fav_flower
password2 = fav_numb   fav_color   fav_numb

print()
print('First password:', password1, 'Second password:', password2)
print()
print(f'Number of characters in {password1}:{len(password1)}', f'Number of characters in {password2}:{len(password2)}')

Result:

You entered: yellow Daisy 6

First password: yellow_Daisy Second password: 6yellow6

Number of characters in yellow_Daisy:12 Number of characters in 6yellow6:8

Another way to go is to tell print() not to add a newline at the end of what it outputs. You do this by adding a parameter end='' to say not to add anything at the end of what it prints. Actually though, you do want to add a space to the end instead of a newline, so you want to add end=' ': Doing it that way, your code would look like this:

print('You entered:', fav_color,fav_flower, fav_numb)
password1 = fav_color   '_'   fav_flower
password2 = fav_numb   fav_color   fav_numb

print()
print('First password:', password1, end=' ')
print('Second password:', password2)

print()
    
print(f'Number of characters in {password1}:{len(password1)}', end=' ')  
print(f'Number of characters in {password2}:{len(password2)}')

This gives the same result as the first example.

I think the first way is more readable, and therefore better. It's easy to miss those end= parameters when reading code. Programmers generally expect to see a line per print().

  • Related