Why Are My Python Print Outputs Different Than the Examples?

0
4
Asked By CuriousCoder123 On

I'm a total beginner trying to learn Python using Mike McGrath's "Coding for Beginners," but I'm facing a bit of confusion with the output formatting. The book's examples use spaces between elements in print functions, and sometimes include tabs (t), but when I run the same code, my output doesn't match the examples. I noticed the book is a couple of years old and I'm using a newer version of Python, which might be affecting how things display.

I really want to grasp good coding practices early on, especially concerning spacing. Can anyone shed light on what might be going wrong and offer advice on how to use the spacebar and tabs effectively in Python coding? Here's my code snippet and the resulting output so you can see what I'm talking about:

```python
a=8
b=4
print('Assign Values:t' , 'a=' , a , 'tb=' , b)
a+=b
print('Assign Values:t' , 'a=' , a , 't(8+=4)')
a-=b
print('Subtract & Assign:t' , 'a=' , a , 't(12-4)')
a*=b
print('Multiply & Assign:t' , 'a=' , a , 't(8x4)')
a/=b
print('Divide & Assign:t' , 'a=' , a , 't(32/4)')
a%=b
print('Modulus & Assign:t' , 'a=' , a , 't(8%4)')
```

And here is the output I got from IDLE:

```
Assign Values: a= 8 b= 4
Assign Values: a= 12 (8+=4)
Subtract & Assign: a= 8 (12-4)
Multiply & Assign: a= 32 (8x4)
Divide & Assign: a= 8.0 (32/4)
Modulus & Assign: a= 0.0 (8%4)
```

2 Answers

Answered By CodeSavvy90 On

It sounds like you're on the right track with your Python journey! A couple of things to consider: the spaces around commas in your print statements don't actually affect how the code runs, so Python doesn't mind that. It’s more about readability. For instance, most programmers write it like this:

```python
print('Assign Values:t', 'a=', a, 'tb=', b)
```

Another thing to keep in mind is that the tab character 't' can display differently based on your terminal settings, which is likely why your output looks different from the book's example. Each tab stop could vary depending on how it's set up, so it might make your output misaligned. Also, know that whitespace at the beginning of lines matters but elsewhere, it’s up to you.

Instead of focusing too much on spacing, try looking into tools like linters that can help you maintain consistency in your code formatting!

Answered By TechieFriend07 On

Congratulations on jumping into coding! Regarding your spacing issues, I’d recommend switching to f-strings for formatting your output, as they'll give you more control. Here’s a snippet:

```python
print(f'{

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.