2016-04-22 51 views
0

Okay, so dass im Grunde, was ich versuche nur während Loops zu erreichen und mit ihnen effizient. Verschachtelte While-Loops sind sehr schwierig für mich und schwer zu verstehen. Ich versuche eine 10x10 Multiplikationstabelle mit einem Header zu erstellen.while-Schleife Multiplikationstabelle Python

Also mein aktueller Code ist:

firstNumber = int(input("Please enter the first number: ")) 
secondNumber = int(input("Please enter the 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 

die Ausgänge:

  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 

der im Grunde richtig ist, aber anscheinend habe ich es nicht richtig machen. Also, wie würde ich die Loops effizienter verschachteln, damit die while-Schleifen immer nur eine einzige Zahl gleichzeitig verarbeiten können statt zehn? Hier

Antwort

0

Ein großes Problem mit dem Code, den Sie geschrieben ist, dass Ihre äußere while Schleife wird eigentlich nur einmal ausgeführt. Da Sie while(count < 1) laufen, und Sie erhöhen count durch ein, nachdem Sie die Schleife einmal ausgeführt, wird diese Schleife verlassen, wenn sein zum zweiten Mal ausgeführt wird.

Während Ihre Antwort die richtige Multiplikationstabelle druckt, bricht es den Geist des Übens while Schleifen durch Drucken von 10 Zahlen mit Ihren manuell codierten Ergebnissen. Eine klügere Methode wäre, das Programm auf 10 zählen zu lassen, anstatt 10 Felder in jeder Zeile, die Sie manuell ausfüllen, wie folgt aufzulisten:

firstNumber = int(input("Please enter the first number: ")) 
secondNumber = int(input("Please enter the second number: ")) 

# Print twelve spaces in order to format the columns correctly, but don't 
# place a newline after the print statement 
print(" "*12, end="") 

# Loop through all of the column header values in the first row 
columnCounter = 0 
while columnCounter < 10: 
    # Print a single incremented column value, with a space separating each 
    print("{:5}".format(firstNumber + columnCounter), end=" ") 
    columnCounter = columnCounter + 1 

# Add a newline to go to the next row 
print("") 

# Print the dashes separating the first row from the rest 
print("{:5} {:}".format(" ", "-"*65)) 

# Print the remainder of the rows in the table 
rowCounter = 0 
while(rowCounter < 10): 
    # Print the left-hand side value 
    print("{:5} {:5}".format(secondNumber, "|"), end=" ") 

    downSolution = firstNumber * secondNumber 

    # Loop through the values for all columns for this row 
    columnCounter = 0 
    while(columnCounter < 10): 
     print("{:5}".format(downSolution + secondNumber*columnCounter), end= " ") 
     columnCounter = columnCounter + 1 

    secondNumber = secondNumber + 1 
    print("") 
    rowCounter = rowCounter + 1 
0

ist ein Hinweis:

row = 1 
row_stop = 10 
while row != row_stop: 
    print(row, end = " ") #end = " " adds a spaceafter each print statement 
          #instead of a new line 
    row += 1 

ausgeben wird

1 2 3 4 5 6 7 8 9 

Die zweite Schleife ist die trickier ein Recht? Sobald Sie am Ende der Reihe sind, wird es bei 11 sein, so dass es nicht weiter schleifen wird. Wenn Sie zwei Schleifen obwohl Sie tun können:

row = 1 
row_stop = 10 
col = 1 
col_stop = 10 
while row != row_stop: 
    wile col != col_stop: 
    #print your column stuff 
    #reset your column counter, 
    #increment your row counter 

Beitrag dieser letzte Teil und ich werde Ihnen helfen debug.