Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying the get the x variable to only run once inside of the loop so it does not delete the table over and over again. I am trying to print a table that looks like this and the user inputs the lower bound(in this example 4) and the upper bound( in this example8).
4: 16 20 24 28 32

5: 20 25 30 35 40

6: 24 30 36 42 48

7: 28 35 42 49 56

8: 32 40 48 56 64


What I have tried:

Python
`

    #Get the inputs
    input3=int(float(input("Give me the lower bound: ")))
    input4=int(float(input("Give me the upper bound: ")))
    #create a loop to multiply the upper and lower bounds 
    multiplier=input4
    multiplication_table_str= " "
    
    for i in range(input4):
        x=" "  
        multiplication_table_str = multiplication_table_str + x
    
        if input3<=input4:
            x=(str(input3)+":" , input3*input3, input3*multiplier)
            input3=input3+1
            multiplier=multiplier+1
    print(multiplication_table_str)
`
Posted
Updated 8-Sep-21 20:01pm
v2

1 solution

First, make your life easier.
Write a Python function[^] that accepts a three parameters (the value to multiply, and the upper and lower bounds) and which returns a string.
Inside the function, use the parameters to create a single row: If you are passed 4, 4, and 8 then it generates this row:
4: 16 20 24 28 32
If you pass 6, 3, and 7 it returns:
18, 24, 30, 36, 42
And so on.
That's pretty easy to do - you have the code, pretty much.
Test it, and make sure it works.

Then set up your loop and call the function inside that, passing it the infor for each row in turn, and printing the returned value.

Presto! It's done.

This may help you to understand, though it's code is written in C#: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900