0

Here is the psuedo code I was supposed to create in python:

PlayerOneScore ← 0
PlayerTwoScore ← 0
OUTPUT "How many games?"
INPUT NoOfGamesInMatch
FOR NoOfGamesPlayed ← 1 TO NoOfGamesInMatch Do
    OUTPUT "Did Player One win the game (enter Y or N)?"
    INPUT PlayerOneWinsGame
    IF PlayerOneWinsGame = 'Y'
        THEN PlayerOneScore ← PlayerOneScore + 1
        ELSE PlayerTwoScore ← PlayerTwoScore + 1
    ENDIF
ENDFOR
OUTPUT PlayerOneScore
OUTPUT PlayerTwoScore

Here is what I created in python and its not working and I don't understand why?

PlayerOneScore = 0
PlayerTwoSCore = 0
NoOfGamesInMatch = input("How Many games?")
for NoOfGamesPlayed != NoOfGamesInMatch:  
    PlayerOneWinsGame = input(" Did Player on win the game(Enter y or N?)")
    if PlayerOneWinsGame == "Y":
        PlayerOneScore = PlayerOneScore + 1
    else:
        PlayerTwoScore = PlayerTwoScore = 1
print("Player one Score is" + str(PlayerOneScore))
print("Player Two Score is" + str(PlayerTwoScore))

I tried the in range part, and I got this error when I input one when the program input how many games.

    for NoOfGamesPlayed in range(NoOfGamesInMatch):
TypeError: 'str' object cannot be interpreted as an integer
11
  • 1
    How is the code not working? What input did you give it, what happened and what did you expect to happen instead? Commented May 30, 2015 at 15:12
  • for NoOfGamesPlayed != NoOfGamesInMatch this have no sense. Commented May 30, 2015 at 15:13
  • for NoOfGamesPlayed != NoOfGamesInMatch is not valid Python code. You tagged this with while-loop, perhaps you meant to use while there? If so, how do you increment your loop counter? Commented May 30, 2015 at 15:14
  • 1
    @Martijn Pieters I tried the in range part, and i got this error when i input one when the program input how many games."for NoOfGamesPlayed in range(NoOfGamesInMatch): TypeError: 'str' object cannot be interpreted as an integer" Commented May 30, 2015 at 15:26
  • 1
    @Danielb7: in future, include such details. It tells us what you already tried, and what went wrong with that. Commented May 30, 2015 at 15:30

3 Answers 3

3

Your line

for NoOfGamesPlayed != NoOfGamesInMatch:  

is not valid Python. If you wanted to use looping here, for is helpful but you need to add a range() function:

for NoOfGamesPlayed in range(int(NoOfGamesInMatch)):

See the Python tutorial on the for construct. Since the input() function returns a string, you need to convert it to an integer first, using the int() function.

Your code otherwise pretty much matches the pseudo-code otherwise, apart from using a lowercase y in your input() line; you may want to correct that as you only test for uppercase Y in the result:

PlayerOneWinsGame = input("Did Player One win the game (enter Y or N?)")

You also made a small typo in your PlayerTwoScore update; replace the second = with +:

PlayerTwoScore = PlayerTwoScore + 1

Putting that together would make:

PlayerOneScore = 0
PlayerTwoSCore = 0
NoOfGamesInMatch = input("How Many games?")
for NoOfGamesPlayed in range(int(NoOfGamesInMatch)):
    PlayerOneWinsGame = input("Did Player One win the game (enter Y or N?)")
    if PlayerOneWinsGame == "Y":
        PlayerOneScore = PlayerOneScore + 1
    else:
        PlayerTwoScore = PlayerTwoScore + 1
print("Player one Score is" + str(PlayerOneScore))
print("Player Two Score is" + str(PlayerTwoScore))
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks this worked a charm. You made a Typo "PlayerTwoSCore", should be "PlayerTwoScore". thanks for the advice
Wouldn't it be better to change entities names according to PEP-0008?
@IgorPomaranskiy: that's not actually going to be helpful here. PEP 8 tells you that you need to stick to the existing conventions; here that means camel-case variables. This is a UK exam question (from June 2010, this student is practicing, not cheating) and that exam uses camel-casing throughout and supports multiple languages, not just Python.
@IgorPomaranskiy: PEP-8 is low hanging fruit; focusing on that is not actually going to help the question asker learn about the actual problems.
@MartijnPieters look, the newbie come for help to community of cool, mature developers. He writes a piece of code which violates a lot of rules, which are supposed to be followed by community. And here we are to the rescue, explaining to him about loops and some other basic things. But keeping all bad approaches untouched and undiscussed. Then he keeps writing bad code, probably until his first real-job interviews. Did we help him, staying focused on his 'actual problems'?
|
1

In every language, for loops are more commonly used to iterate over a range of value as in

for record in records
for file in files
for i in range(0, 10)
for prime_number in [11, 13, 19]

On the other hand, while loops are used to perform blocks of code while a given condition evaluates to true

while i_am_hunger: eat()
while list_is_empty
while list_is_not_empty

And so on.

It seems to me that your case fits more in a while loop. Something like:

while NoOfGamesPlayed != NoOfGamesInMatch:  *do something*

Last note: Python has some style guides that are intended to keep your code cleaner. While style is sometimes a personal choice, it would be nice if you take some time to read them. For instance, in your case, your variable names should be divided by underscore as in no_of_games_played. Checkout more here:

Google Style Guide

PEP-8

2 Comments

For what it's worth, the mark scheme (page 27) for this exam expects the variables to be using the original camel casing
Good to know! Well, I did not know this question was for a test. OP, adhere to the given naming convention in this case
-1

If I've guessed your intentions, the code should look like this:

player_one_score = 0
player_two_score = 0

games_in_match = input("How Many games?")

for i in range(games_in_match):  
    player_one_wins = input(" Did Player One win the game(Enter Y or N?)")
    if player_one_wins.upper() == "Y":
        player_one_score += 1
    else:
        player_two_score += 1

print("Player One Score is {}".format(player_one_score))
print("Player Two Score is {}".format(player_two_score))

5 Comments

The title of the question suggests that they should be using for and range, not while. Without an explanation, this isn't very helpful for the OP; don't just give them code. Why does this work? Where did they go wrong?
Updated the code. I believe that working code is much better (and quick) tutorial point, than dealing with contains tons of errors. OP can compare the versions and find out what has changed.
Now imagine the teacher reading this and asking the student about how they got this lovely code, and could they explain how it works? What does the format() call do? Where did they read about the augmented assignment syntax? And the student would not be able to tell them any of that.
Your code gives them a fish, while they were really asking about how to fish.
I suppose that students are smart enough to google for unknown code entities. Besides that, they can just to play with the working code and try to apply different approaches. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.