Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is my While Loop not looping?

+3 votes
asked Mar 3, 2023 by ANewCoder (290 points)
here's my code:

x = int(input("How many rows to you want to print? "))

i = 0

print("Example: 3 -")
print("Output: ---")

while i < x:
    inp = input()
    result = ""
    final = inp.split(" ")
    for i in range(1, ((int(final[0]) - 1) + 2)):
        result = result + (final[len(final) - 1])
    i += 1


Why is my while loop not working?

I'm trying to finish my homework, that is coding

question Problem J2 in this PDF



1 Answer

0 votes
answered Mar 3, 2023 by Peter Minarik (88,520 points)

First of all, the inputs do not come separated by spaces, but in new lines. So inp.split(" ") will not separate the 3/2/1 scores into final.

You should follow the instruction you shared through the linked PDF.


    for i in range(1, ((int(final[0]) - 1) + 2)):
        result = result + (final[len(final) - 1])

is weird as well.

Here's what you should do:

  • Read 3 lines. The first will contain the 3, the second the 2, and the third the 1-point shot counts.
  • Then you calculate the total score by multiplying the counts (read from the three lines) with the score values (3/2/1).
  • You do it for both teams (A and B) and print A or B depending on who has the higher score. Or T if it's a tie.
  • When done, test it with the inputs provided in the PDF (example section). Then test it for more inputs on your own. Test for corner cases. Test if you determine the win of A, B, or even the tie.
commented Mar 3, 2023 by ANewCoder (290 points)
Thank you so much! It works now!
commented Mar 4, 2023 by Peter Minarik (88,520 points)
I'm glad you're sorted. :)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.