@virnuls/

Number Table

Python

One of the OCR Coding Challenges

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
operators = ("+","-","/","*")
operator = ""
# input the operator
while operator not in operators:
  operator = input("Please enter an operator: +, -, * or /: ")
size = 0
# input the size and ensure it's a positive integer
while size < 1:
  try:
    size = int(input("Please enter a natural number: "))
  except:
    print("That's not a number.")
    size = 0
#heading
row = "\n" + operator + "\t|"
for x in range(size + 1):
  row += "\t" + str(x)
print(row)
# underline the heading
print("-"*((size+2)*4+3))
# rest of table
for y in range(size + 1):
  # using \t to space out the table
  row = str(y) + "\t|"
  for x in range(size + 1):
    # trap invalid calculations, e.g. dividing by zero
    try:
      row += "\t" + str(round(eval(str(y)+operator+str(x)),1))
    except:
      row += "\t-"
  print(row)