Question

In Python, write a program that asks the user for a positive integer number. If the...

In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this.
https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

0 0
Add a comment Improve this question Transcribed image text
Answer #1
while 1:
    # asking and validating number
    num = input("Enter the number or -1 to quit: ")
    if num == '-1':
        exit()
    elif not num.isdigit():
        print("invalid number ")
    else:
        num = int(num)
        count = 0
        x = 1
        y = 0
        # number of table printed per column
        col_size = 5
        print()
        while num > 0:
            # deciding the column size
            if not count:
                if num >= col_size:
                    y = col_size
                    num -= col_size
                else:
                    y = num
                    num = 0

            if count:
                x = y + 1
                if num >= col_size:
                    y = y + col_size
                    num -= col_size
                else:
                    y = y + num
                    num = 0
            # printing the table
            for i in range(1, 11):
                for j in range(x, y + 1):
                    print("{:<3} x {:<2} = {:<3}".format(j, i, (i * j)), end="  ")
                print()
            print("\n")
            count += 1

# OUTPUT

Enter the number or.-l.to quit: invalid number Enter the number or.-l.to quit: 1x2=2 2x2=4 3x2=6 4x2=8 5x2=10 1x3=3 2x3=6 3x3

Add a comment
Know the answer?
Add Answer to:
In Python, write a program that asks the user for a positive integer number. If the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Programming 4th Edition: Write a program that asks the user for the number of feet....

    Python Programming 4th Edition: Write a program that asks the user for the number of feet. The program converts those feet to inches by using the formula ’12 * feet’. Hint: Define main () program Get the number of feet from the user Define variables Call a function feet_to_inches(feet) The function receives the number of feet and returns the number of inches in that many feet. The output should look like as follows: Enter the number of feet: If the...

  • 4- Write a program by using function that asks the user for entering a positive integer...

    4- Write a program by using function that asks the user for entering a positive integer number (a) and then calculate the factorial of the number. Draw the Trace Table (8 Marks)( k/2, T/2, C/2, A/2) Enter a number: 4 Factorial (4) = 4! =4*3*2*1=24 use python pls

  • using python Write a program that asks the user to enter a month (1 for January,...

    using python Write a program that asks the user to enter a month (1 for January, 2 for February, and so on) and then prints the number of days in the month. For February, print “28 or 29 days”. Enter a month: 5 30 days Do not use a separate if/else branch for each month. Use Boolean operators.

  • need this by tonight pls using python Write a program that asks the user to enter...

    need this by tonight pls using python Write a program that asks the user to enter a month (1 for January, 2 for February, and so on) and then prints the number of days in the month. For February, print “28 or 29 days”. Enter a month: 5 30 days Do not use a separate if/else branch for each month. Use Boolean operator

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • Please solve it by Python 3 Write a program that asks the user for a positive...

    Please solve it by Python 3 Write a program that asks the user for a positive integer limit and prints a table to visualize all factors of each integer ranging from 1 to limit. A factor i of a number n is an integer which divides n evenly (i.e. without remainder). For example, 4 and 5 are factors of 20, but 6 is not. Each row represents an integer between 1 and 20. The first row represents the number 1,...

  • Using python Problem 6 (10 points). Write a program which asks the user to enter a...

    Using python Problem 6 (10 points). Write a program which asks the user to enter a number and prints its multiplication table upto 10. Sample program output: Please enter a number: 9 9 X 1 = 9 9 X 2 = 18 9 X 3 = 27 9 X 4 = 36 9 X 5 = 45 9 X 6 = 54 9 X 7 = 63 9 X 8 = 72 9 X 9 = 81 9 X 10...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Write a script that asks the user to input a number. If the value the user...

    Write a script that asks the user to input a number. If the value the user enters is a negative value, call the pre-defined function neg_value which consumes no arguments and returns no values. If the user enters a positive value, call the pre-defined function pos_value which consumes no arguments and returns no values. If the user enters a zero, display the message "Invalid Value". Doing this on Python. This is what I have: number = int(input("Enter a number: "))...

  • Write a program using M.I.P.S that asks user “how many positive number that is devisable by...

    Write a program using M.I.P.S that asks user “how many positive number that is devisable by 6 you want to add?” .Then your loopcounter would be the user input. If the user enters a positive number between 1 and 100 that is devisable by 6, you increment your loop counter and add it to the sum.. You need to decide if the positive number entered by the user is divisible by 6 or not. Your program should print an error...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT