3

I'm taking a class through zybooks, and the whitespace is a bit glitchy through here. I'm not sure what is causing this extra space, so any help would be appreciated.

The instructions for this:

Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Note: 200 is not a valid auxiliary highway because 00 is not a valid primary highway number.

Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west.

highway_number = int(input()) 
if highway_number == 0:
    print(highway_number, 'is not a valid interstate highway number.')
if highway_number in range(1,99+1):
     if highway_number % 2 == 0:
         print('I-',highway_number,"is primary, going east/west.")
     else:
          print('I-',highway_number,"is primary, going north/south.")
else:
  served = highway_number % 100 
  if highway_number >= 1000:
    print(highway_number,'is not a valid interstate highway number.')
  if highway_number in range(99,999+1):
     if highway_number == 200:
       print(highway_number,'is not a valid interstate highway number.')
     elif highway_number % 2 == 0:
         print('I-',highway_number,'is auxiliary, serving I-','%.f,'%served,'going east/west.')
     else:
          print('I-',highway_number,'is auxiliary, serving I-','%.f,'%served, 'going north/south.')
    
             
    
             

Picture for reference of output

Everything is working correctly, I just keep getting an extra space after the '-' in 'I-'.

5 Answers 5

7

That is how print works. It add space between every argument. That is why

print(1,2,3)

prints 1 2 3 not 123.

So, most of the time, you want that behavior.

That being said, you can change it

print(1,2,3, sep='')

prints 123

In your case (not yet the solution)

print("I-", highway_number,"is primary, going east/west.", sep='')

But, see, now you get

I-90is primary, going east/west.

So, you see, yourself, you want that space. Just not everywhere. If you added sep='' now, you have to add explicitly the spaces you want

print("I-",highway_number," is primary, going east/west.", sep='')

Another method would be to print a f-string, in which you can insert values

print(f"I-{highway_number} is primary, going east/west.")

Or mixture of it

print(f"I-{highway_number}", "is primary, going east/west.")

Or some variants (there are many way in python to prints string containing values)

print("I-%d is primary, going east/west."%(highway_number,))
print("I-{} is primary, going east/west.".format(highway_number))

But the fastest and easiest way are f-string (they are also the most recent ones, which explain why the other exist while not being as easy and as fast. Plus others methods can do more things, but none that concern our case)

0
1

In print() python if you add , between variables it adds a space by default. To avoid this you can change the , with + (change the type of variable to string for this case)

print("Hi",".") # Hi .
print("Hi"+".") #Hi.

Alternatively, you can also use f-strings for easy management.

a = "."
print(f"Hi{a}") #Hi.
print("Hi{0}".format(a)) #Hi.
1

Use

print('I-',highway_number,"is primary, going east/west.", sep="")

To remove the added space between each item of print. sep is short for separator. More detail: https://docs.python.org/3/library/functions.html#print

8
  • Why use sep when you could use f-strings?
    – The Myth
    Commented Nov 1, 2022 at 12:52
  • I simply mean to provide them with the best technique rather than just a technique.
    – The Myth
    Commented Nov 1, 2022 at 12:56
  • 2
    Both are valid, but it seems to me @zel does not know about f-strings, and they are a more used and practical choice than sep and can be used in a different variety. The use of , to add a space is a bad practice, and adding a sep makes it worse. What if a space was required in one place, but not in the other Commented Nov 1, 2022 at 12:56
  • 2
    That being said, if you add a sep='' in your print, then you must also add the space you wanted. There are 2 implicit spaces in this print. The first one was unwanted. The second, after the number, was wanted. So, sep='' is a perfectly valid way to remove the first. But then you must put back explicitly the second that is also removed. In other words, you may want to edit your answer to add a space before "is primay...".
    – chrslg
    Commented Nov 1, 2022 at 13:05
  • 1
    I feel sep adds unecessary confusion as spaces are to be adjusted based on whether they are wanted or not. f_strings exactly display where the space is in one look Commented Nov 1, 2022 at 13:06
1

In this line of code,

print('I-',highway_number,"is primary, going east/west.")

the , adds a space. Instead, use f-strings

print(f"I-{highway_number} is primary, going east/west.")
-3

Python can be very confusing and sometimes the littlest things will change what it does. In this case, you have to change the comma next to the I- to a + sign. You then have to change the variable highway_number to a string. For example print('I-'+ str(highway_number) + " is primary, going east/west.")

5
  • Please format your code.
    – The Myth
    Commented Nov 1, 2022 at 12:55
  • 1
    Good explanation, but not the best practice and please format your code. Commented Nov 1, 2022 at 12:58
  • May I ask how to format it?
    – Kwallcoder
    Commented Nov 1, 2022 at 12:59
  • 1
    For example, the highway_number can be written like so fenced in backticks, and a new line can be added for your solution. Commented Nov 1, 2022 at 13:00
  • Thank you, is it formatted now?
    – Kwallcoder
    Commented Nov 1, 2022 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.