#πŸ”’ Need some assistance with get a loop to work

233 messages Β· Page 1 of 1 (latest)

near plover
#

I've attached an image on what the output should look like. I'm very new to python and idk how functions work, so It would be great to stay away from that. Right now my code can detect if a number is above 180 or below 0 but only once, then the loop stops. Here's what I've got so far

p1 = int(-1)
print("Please enter the scores for player 1:")
while p1 > 180 or p1 < 0:
    p1=int(input())
    if p1 > 180 or p1 < 0:
        print("Invalid score. Please try again.")
        p1 = int(-1)
    if p1 < 180 or p1 > 0:
        break
green lindenBOT
#

@near plover

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

torn jackal
#

You don't need to do int(-1) btw.

#

-1 is already an int, so no point to converting -1 to an int. Python already knows the type.

near plover
#

right okay

torn jackal
#

What is the entire assignment, because this shows only part of it

#

You are just supposed to create a program that creates the promts shown in the green?

near plover
#

yes

torn jackal
#

Getting replies and handling wrong inputs, yes?

#

alright

near plover
#

theres a part 1 i can show you the code and breif for the if it helps

torn jackal
#

When should it stop asking for scores?

near plover
#

after the third valid input

torn jackal
#

Okay, so always three scores

#

And can you use a for loop?

#

Or it needs to be while?

near plover
#

it can be anything but i've never used a for before

#

i'm fine to try it i jus needa learn sm more abt it

torn jackal
#

Ah while might actually make more sense here

#

Can you show the full assignment, wondering if you can code it from scratch or you have to continue code that they give

near plover
#

the full assignment is 50 other different coding projects that get harder

#

but i can show you the first dart competition project code as this stems from that

torn jackal
#

Yes

#

Did you have to create that yourself?

#

Or was it given partially

near plover
#

myself

torn jackal
#

Otherwise we can just start from scratch for simplicity, it won't be that hard

#

Alright

near plover
#

yes we can code from scratch

torn jackal
#

So first of, we want to keep looping until we have gotten three valid inputs

near plover
#

heres the code from the first project:

print("Please enter the scores for player 1:")
p1=int(input())
p2=int(input())
p3=int(input())
print("Please enter the scores for player 2:")
a1=int(input())
a2=int(input())
a3=int(input())
toatal_player1=(p1+p2+p3)
toatal_player2=(a1+a2+a3)

print("Player one scored",toatal_player1)
print("Player two scored",toatal_player2)

if toatal_player1 > toatal_player2:
    print("Player one wins!")
else:
    print ("Player two wins!")
torn jackal
#

So the basis would be something like

valid_inputs = 0
while valid_inputs < 3:
  ...
near plover
#

what are the 3 dots at the end for?

torn jackal
#

Just for me to show that we will put code there

#

Placeholder

near plover
#

right

#

oh yes i see

#

so for every valid input we add one to the variable

torn jackal
#

So before the loop starts, we should probably ask "Please enter scores for player 1"

#

Exactly

near plover
#

okay

torn jackal
#
print("Enter scores for player 1:")
valid_inputs = 0
while valid_inputs < 3:
  ...
#

So this would be the basis for the loop for player 1

#

Then each iteration, we ask for a score --> Check if this score is valid. If it is valid, add the score to some total score for player 1, and add 1 to valid inputs.

near plover
#

let me try smth you tell me if that would work

torn jackal
#

If it is not valid, we need to prompt the user that this score was not valid, and not add this number to the counter.

torn jackal
near plover
#
print("Enter scores for player 1:")
valid_inputs = 0
while valid_inputs < 3:
score = input()
if score <= 180 or score > 0:
  valid_inputs + 1
else:
  print("Invalid score try again")
  ...

#

hows that?

torn jackal
#

This would give an error pretty sure

#

Remove the ... and try running it

near plover
#

i'll try it in vs code one sec

#

yea theres an error for the score variable

#

'IndentationError: expected an indented block after 'while' statement on line 3'

#

oh right i see

torn jackal
#

Right, what does that mean do you think?

near plover
#

it needs a indentation because of the colon after the while loop

torn jackal
#

Not really "because of the colon", but because you want to separate all the code that is in the "while loop body"

#

So all code that you want to loop needs to be indented one level further than the while statement.

near plover
#

so if i indent the score variable will that be fine or do i need to indent everything after the by 1 more to?

torn jackal
#

So for every iteration (ideal case 3 times) we need to ask the score

#

So every iteration this needs to be asked. It needs to be in this while loop we created.

near plover
torn jackal
#

In this example line 1 and line 2 are inside the while loop body.

#

line 3, 4, 5 are not indented anymore

#

So they do not get looped

#

Stuff you want to do once (like setting valid_inputs=0, and score_player1=0) should thus be before the while loop, or inside the while loop?

near plover
#

so this?

print("Enter scores for player 1:")
valid_inputs = 0
while valid_inputs < 3:
  score = input()
  if score <= 180 or score > 0:
    valid_inputs + 1
  else:
    print("Invalid score try again")
  ...
torn jackal
#

Better yes

#

still some problems

#

Or actually one

#

valid_inputs + 1

#

What do you want to do here?

#

Say it in english

near plover
#

i want to add 1 to the valid input variable so for every good input it will add to 3

#

then the loop will be true

torn jackal
#

Right, so you want to change the value of valid_inputs to a value that is 1 higher

near plover
#

yes

torn jackal
#

so valid_inputs = ... ?

near plover
#

1?

torn jackal
#

That would set the number of valid_inputs to 1

near plover
#

or +1 maybe

torn jackal
#

You want to set it to 1 value higher than it is currently

#

Which is?

near plover
#
  • 1
torn jackal
#

+1 is still 1

near plover
#

but it goes from 0 to one

torn jackal
#

Say you have a value x. Now what value is one higher than x?

near plover
#

2x

torn jackal
#

2 times x?

#

2 --> 3, so 3 = 2 + 1

#

x --> one value higher than x, so x = ...

#

You are thinking too complicated πŸ˜›

near plover
#

i def am im so lost

torn jackal
#

What value is 1 higher than x?

near plover
#

1 value higher than x is 2x

torn jackal
#

No, that would be two times x

near plover
#

so 2 + x?

torn jackal
#

x + 1

near plover
#

oh right that simple

torn jackal
#

yeah haha

#

So, valid_inputs = ... ?

near plover
#

valid_inputs + 1?

torn jackal
#

You need to et this value to one higher than it currently is

#

exactly!

near plover
#

righttt!

#

that makes sense

torn jackal
#

If you just did valid_inputs = 1, it would keep setting it to 1 for each input

#

Also the second, third, fourth etc. Infinite loop

near plover
#

so it would be

print("Enter scores for player 1:")
valid_inputs = 0
while valid_inputs < 3:
  score = input()
  if score <= 180 or score > 0:
    valid_inputs = valid_inputs + 1
  else:
    print("Invalid score try again")
  ...
torn jackal
#

Right, and you don't need to add the three dots, it was just a placeholder for my example snippet.

#

Now try it out, see if it works like you expect it to

near plover
#

okay!

torn jackal
#

And you are still missing a little bit (remember you want to keep track of the total score that player 1 has obtained)

near plover
#

it needs to be an int

torn jackal
#

Oh right, missed that one

#

That is why we check it πŸ˜„

#

So your fix is?

near plover
#
score = int(input())
torn jackal
#

jup, good

#

Does it work like you expect it to for now?

near plover
#

nope it still isnt reigistering if it is over 180

#

oh thats on me

#

i used the wrong >

#

wait no i didnt

torn jackal
#

So let's take an example

#

190

#

score <= 180 or score > 0

#

FIrst we check 190 <= 180 ?

#

Is that true?

near plover
#

no

torn jackal
#

So we have False or score > 0

#

190 > 0, is that true?

near plover
#

i fixed it

#
print("Enter scores for player 1:")
valid_inputs = 0
while valid_inputs < 3:
  score = int(input())
  if score >= 180 or score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
#

i jus switched them around

torn jackal
#

Jup that works

#

Could have also changed the initial if to if score >= 0 and score <= 180

#

To check if it is within bounds

near plover
#

and is a thing?

torn jackal
#

Yes,

near plover
#

wow okay!

torn jackal
#

X and Y is only True if both X and Y are True.

near plover
#

ohhh and because its an or only one needed to be true

#

and because it was always gonna be above zero or below 180 it would always be true for one of them at least

torn jackal
#

You could have even did if not (score < 0 or score > 180). Which is the same as if score >= 0 and score <= 180

torn jackal
#

Check the edge cases

#

(180)

near plover
#

whats an edge case

torn jackal
#

Like an input that might mess up your program

#

Often on the edge of values that would be correct, or 0, that kinda stuff

#

Should 180 be deemed a valid input?

near plover
#

yes

#

so remove the >= and make it jus >

torn jackal
#

Yes

near plover
#

and make <= 0 so it cant be zero

torn jackal
#

Is 0 valid?

#

(maybe the person misses the board with a dart)

near plover
#

oh right yea!

#

good shout

torn jackal
#

So 0 should be in the valid range

near plover
#

yes

torn jackal
#

So < is already correct

near plover
#

okay so the loop seems to be working

torn jackal
#

It's only invalid below 0

#

Yes

near plover
#

should i add it to the code now?

#

or do i need to check smth else

torn jackal
#

This is the code we hace so far, and it works, it takes inputs, and checks if it is correct.

#

We now also need to keep track of the total score.

#

Which, before throwing, starts at 0, and increases after every valid throw.

#

So add that to this code.

near plover
#

right okay

#

but what would the code be?

#

the only thing i can think of is weird

#

like this

valid_inputs = 0
while valid_inputs < 3:
  score = int(input())
  if score > 180 or score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
    score = score + score
torn jackal
#

So it's pretty close. But we do not change the value of score, because we use this to store only the number of the "current" dart throw.

#

So we keep overwriting it after each throw.

near plover
#

so

current = score + score
torn jackal
#

Therefore, we might need to ad another variable to keep track of the total score, similar to how we keep track of the number of valid throws.

#

But we do not increment this with 1 each throw, but with the score of the current dart throw.

near plover
#
current = current + score
torn jackal
#

And current would then keep track of the total score?

near plover
#

well id assume so

torn jackal
#

Alright

near plover
#

okay

torn jackal
#

And you need to add another line

near plover
#

what would it be for?

#

if its to print the score thats done later in the code

torn jackal
#

RIght, but we want to keep track of the score in this loop

#

How else would we get this information later in the code

near plover
#

print(current)

torn jackal
#
valid_inputs = 0
while valid_inputs < 3:
  score = int(input())
  if score > 180 or score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
    current = current + score

print(current)
#

run it

near plover
#

so i needed to add ```py
current = 0

torn jackal
#

yes

near plover
#

at the start outside of the loop

#

i see!

torn jackal
#

Now try it out, see if it works

near plover
#

yes i worked

torn jackal
#

and imo, I would rename current to total_score_player1

#

That is more descriptive

near plover
#

gotcha

torn jackal
#

So, now do this for player 2

#

It should be pretty easy to do, once you have done it for player 1

near plover
#

ive already started

#

heres what ive got so far

#

print("Please enter the scores for player 1:")
valid_inputs = 0
total_score_player1 = 0
while valid_inputs < 3:
  p1Score = int(input())
  if p1Score > 180 or p1Score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
    total_score_player1 = total_score_player1 + p1Score


print("Please enter the scores for player 2:")
valid_inputs = 0
while valid_inputs < 3:
  p2score = int(input())
  if p2score > 180 or p2score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
#

obviously we need the ```py
total_score_player1 = total_score_player1 + p1Score

torn jackal
#

We want to store the total score of player 1 and player 2 in separate variables

near plover
#

yea ik

#

ive done that

#

print("Please enter the scores for player 1:")
valid_inputs = 0
total_score_player1 = 0
while valid_inputs < 3:
  p1Score = int(input())
  if p1Score > 180 or p1Score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
    total_score_player1 = total_score_player1 + p1Score


print("Please enter the scores for player 2:")
valid_inputs = 0
valid_inputs = 0
total_score_player2 = 0
while valid_inputs < 3:
  p2score = int(input())
  if p2score > 180 or p2score < 0:
    print("Invalid score try again")
  else:
    valid_inputs = valid_inputs + 1
    total_score_player2 = total_score_player2 + p2score
#

finished

torn jackal
#

Looks pretty good. Accidentally typed valid_inputs = 0 twice there for player 2

#

But it should still work correctly nonetheless

near plover
#

it works

#

now i need to print the scores and compare who won then i should be done

torn jackal
#

Only remark I can give that could be improved is naming convention. Python uses snake_case, not camelCase or PascalCase.

#

But this is just a detail.

near plover
#

right i see

#

okay it all works

#

Camel tysm you helped a load

torn jackal
#

cool nws!

near plover
#

i'll def be making another post tho theres even harder challenges but you taught me a lot so i might be able to get them done

#

okay bye!

green lindenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.