while loop multiplication table python -
okay trying accomplish using while loops , using them efficiently. nested while loops tricky me , hard understand. trying make 10x10 multiplication table header.
so current code is:
firstnumber = int(input("please enter first number: ")) secondnumber = int(input("please enter second number: ")) count = 0 while(count < 1): print("{:17} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}"\ .format(firstnumber, firstnumber + 1, firstnumber + 2, firstnumber + 3,\ firstnumber + 4, firstnumber + 5, firstnumber + 6, firstnumber\ + 7, firstnumber + 8, firstnumber + 9)) print("{:5} {:}".format(" ", "-"*65)) count += 1 counter = 0 while(counter < 10): downsolution = firstnumber * secondnumber print("{:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5} {:5}"\ .format(secondnumber, "|", downsolution,\ downsolution + secondnumber, downsolution +\ (secondnumber * 2), downsolution + (secondnumber * 3),\ downsolution + (secondnumber * 4), downsolution + \ (secondnumber * 5), downsolution + (secondnumber * 6),\ downsolution + (secondnumber * 7), downsolution + \ (secondnumber * 8), downsolution + (secondnumber * 9))) counter += 1 secondnumber += 1
which outputs:
5 6 7 8 9 10 11 12 13 14 ----------------------------------------------------------------- 5 | 25 30 35 40 45 50 55 60 65 70 6 | 30 36 42 48 54 60 66 72 78 84 7 | 35 42 49 56 63 70 77 84 91 98 8 | 40 48 56 64 72 80 88 96 104 112 9 | 45 54 63 72 81 90 99 108 117 126 10 | 50 60 70 80 90 100 110 120 130 140 11 | 55 66 77 88 99 110 121 132 143 154 12 | 60 72 84 96 108 120 132 144 156 168 13 | 65 78 91 104 117 130 143 156 169 182 14 | 70 84 98 112 126 140 154 168 182 196
which correct apparently didn't correctly. how nest loops more efficiently while loops deal 1 single number @ time instead of ten?
one major problem code posted outer while
loop run 1 time. since run while(count < 1)
, , increment count
1 after run loop once, loop exit when running second time.
while answer print correct multiplication table, kind of breaks spirit of practicing while
loops printing 10 numbers manually coded results. smarter way let program count 10 you, instead of listing 10 fields in each row manually fill in, this:
firstnumber = int(input("please enter first number: ")) secondnumber = int(input("please enter second number: ")) # print twelve spaces in order format columns correctly, don't # place newline after print statement print(" "*12, end="") # loop through of column header values in first row columncounter = 0 while columncounter < 10: # print single incremented column value, space separating each print("{:5}".format(firstnumber + columncounter), end=" ") columncounter = columncounter + 1 # add newline go next row print("") # print dashes separating first row rest print("{:5} {:}".format(" ", "-"*65)) # print remainder of rows in table rowcounter = 0 while(rowcounter < 10): # print left-hand side value print("{:5} {:5}".format(secondnumber, "|"), end=" ") downsolution = firstnumber * secondnumber # loop through values columns row columncounter = 0 while(columncounter < 10): print("{:5}".format(downsolution + secondnumber*columncounter), end= " ") columncounter = columncounter + 1 secondnumber = secondnumber + 1 print("") rowcounter = rowcounter + 1
Comments
Post a Comment