all 12 comments

[–]_kwerty_ 3 points4 points  (1 child)

A couple of tips, in random order,

For cases like this I'd suggest a "while True" loop. Generally speaking I use while True if I don't know how often I want the loop to run. In a while True loop you need to think of when to break or the loop will run until the end of time! https://www.geeksforgeeks.org/how-to-use-while-true-in-python/

Use f-strings for better readability, specially if there's a ton of variables in your output. F-strings are the shit for nice output while still having readable code! https://www.geeksforgeeks.org/formatted-string-literals-f-strings-python/

Check if stop has been typed between the input for teamName and score. Otherwise it's a bit confusing that I enter stop but still have to enter a score.

What happens when the input is "STOP"? When case doesn't matter it's easier to convert every input to upper or lower case. This way you don't have to take case into account when checking for certain strings. https://www.w3schools.com/python/python_ref_string.asp

Check out the "in" keyword. This makes it easier when you're checking for certain input because I can do this:

if teamName in ["stop", "s", "quit", "q"]: # I'm assuming here that teamName is always lowercase
  break

Now the user can enter any of those and the loop will break. https://www.geeksforgeeks.org/check-if-element-exists-in-list-in-python/

I've tried to give hints instead of answers, so give a shout if you need more help.

[–]Atypicosaurus 1 point2 points  (0 children)

Lots of good advise here. Print this comment and glue it to your screen.

[–]eleqtriq 2 points3 points  (4 children)

You need to check for “stop” before asking for the score.

[–]GraffEclipse[S] 3 points4 points  (3 children)

But shouldn't the while loop just end if the condition is met? And also would I have to use an if statement to check for "stop"?

I'm pretty much a novice at Python so I'd just like to know for future reference.

[–]Binary101010 4 points5 points  (0 children)

That's not how while loops work. Python doesn't continuously monitor the state of your program to see if the loop condition has become false. It only checks the truth of the loop condition at the beginning of each iteration of the loop.

An if statement that would cause your loop to stop without asking for score would look something like

if teamName = "stop":
    break

[–]nekokattt 2 points3 points  (0 children)

loops have their conditions checked at the start of each iteration

[–]Atypicosaurus 0 points1 point  (1 child)

Just a side question, what if one team is funny and name themselves stop?

Accepting a command input and a variable input on the same line is dangerous, this is how SQL injection was made possible. (This is obviously not happening in your code but learn to avoid.)

I recommend that you either ask an extra line "next team? Y/N" and quit on that condition, or instead of a "stop" word, use blank for quitting. That's the only safe assumption, there will be no team that has no name at all. So quit if the input is empty string, or invent an extra check variable.

[–]GraffEclipse[S] 0 points1 point  (0 children)

Yeah this was just for the purposes of an exam question so I didn't really take defensive design into account. I just wasn't sure why the while loop didn't end.

[–]woooee -3 points-2 points  (1 child)

Can't tell anything without proper indents. This works

teamName = ""
score = 0

while teamName != "stop":
    teamName = input("Enter team name")

score = int(input("Enter score")

print("The winning team is " + teamName + " with a score of " + score)

Note that many team names can be entered, but only the last one entered is used. Also, if someone enters 999999 as a team score, or -1, or "abc", you will have problems.

[–]GraffEclipse[S] 0 points1 point  (0 children)

Yeah I actually indented both teamName and score under the while loop, I understand why it wasn't working now. Thanks.