0

I am attempting to create a program asking the user for two inputs, from this a multiplication table will be created. For example, the user inputs 2 and 5.

Enter a starting integer of less than 1,000 and greater than 0: 2

Enter an ending integer greater than the first number and less than 1,000 : 5

I get something that looks like this:

     4     6     8    10

     6     9    12    15

     8    12    16    20

    10    15    20    25

However the math is wrong and I want 2-5 printed at the top and on the left side.

This is what I have so far:

    # In this program I will help you make a multiplication table
    print('In this program, I will help you make a multiplication table.')
    print('\n')

    # Ask user for a starting integer 
    start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
    while start_value < 1 and start_value > 1000:
        start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))

    # Ask user for an ending integer
    end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
    while end_value < 0 and end_value > 1000 and end_value > start_value:
        print('Invalid number')
        end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))

    for num1 in range(start_value, end_value+1):
        for num2 in range(start_value, end_value+1):
            table = (num1*num2)
            print(format(table, '6d'), end = '')
        print('\n')

I feel like something is up with the for-loop, I hope this makes sense, I greatly appreciate all the help!

Thank you!!!

Felisha V
  • 1
  • 4
  • 1
    You've told the user they've entered an invalid number. But then you don't give them an opportunity to change it. You just repeatedly write. "Invalid number". "invalid number". Simplest fix is to just add another input statement after your two print statements. – Frank Yellin Sep 30 '20 at 19:34

2 Answers2

1

In this code here, you get input, then start a loop. The input function is what gets user input, and you never get it again. You also have a logic error with your ands, they should be ors instead.

# this is your code, exactly as above
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 and end_value > 1000 and end_value > start_value:
    print('Invalid number')

Easiest fix:

end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 or end_value > 1000 or end_value > start_value:
    print('Invalid number')
    end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))

But that duplicates the first line. So, a couple of other options:

while True:
    end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
    if end_value < 0 or end_value > 1000 or end_value > start_value:
        break
    print('Invalid number')
   

Or, if you are on python 3.8+, you can use the := syntax here

while (end_value := int(input('Enter an ending integer greater than the first number and less than 1,000 : '))) < 0 or end_value > 1000 or end_value > start_value:
    print('Invalid number')
   
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
0

After working through this for a while, I did come up the answer to my own question. I want to post it here for anyone else who may be interested in the solution.

    # Max value as a constant
    MAX_VALUE = 1000

    # In this program I will help you make a multiplication table
    print('In this program, I will help you make a multiplication table.')
    print('\n')

    # Ask user for a starting integer and give parameters 
    start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
    while start_value < 1 and start_value > MAX_VALUE:
        start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
    print('\n')

    # Ask user for an ending integer and give parameters
    end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
    while end_value < 0 and end_value > MAX_VALUE and end_value > start_value:
        print('Invalid number')
        end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
    print('\n')

    # Format the top corner space
    print('      ', end = '')

    # Add a label row
    for label in range(start_value, end_value +1):    
        print(format(label, '6d'), end = '')
    print('\n')

    # Multiplication chart and left label column 
    for num1 in range(start_value, end_value+1):
        print(format(num1, '6d'), end = '')
        for num2 in range(start_value, end_value+1):
            table = (num1*num2)
            print(format(table, '6d'), end = '')
        print('\n')

This makes a clean Multiplication table for any range of numbers less than 1,000.

Cheers!

Felisha V
  • 1
  • 4