Search code examples
pythonvscode-python

Generating random numbers in Python using random.randrange(). After the final number is generated, a "%" is created. How can I fix this?


import random

# Main menu.
print('**** Welcome to the Pick-3, Pick-4 lottery number generator! ****\n\
    \nSelect from the following menu:\n\n1. Generate 3-Digit Lottery number\
    \n2. Generate 4-Digit Lottery number\n3. Exit the Application\n')

# Holds the user's selection in memory.
userInput = int(input())

# If-else statements performs proper operation for the user.
if userInput == 1:
    print('\nYou selected 1. The following 3-digit lottery number was generated:\
        \n\n')
    for i in range(3):
        print(random.randrange(1, 10), end = '')

if userInput == 2:
    print('\nYou selected 2. The following 4-digit lottery number was generated:\
        \n\n')
    for i in range(4):
        print(random.randrange(1, 10), end = '')

if userInput == 3:
    print('\nYou selected 3.\n\nThanks for trying the Lottery Application.\
        \n\n*********************************************************')
    SystemExit

This is the output I get when I enter 1, for example:

You selected 1. The following 3-digit lottery number was generated:

657%

How can I get rid of this percentage sign? I am using VSCode to compile. Thanks.


Solution

  • The % is not output from Python. It's your shell telling you there wasn't a trailing newline (or, rather, that the cursor was left at a nonzero column offset; your shell can't see what output the programs it ran wrote when that output wasn't captured or redirected, but it can query the terminal for cursor position). Your last print before exiting shouldn't have end=''.