Danielb7
Danielb7

Reputation: 3

Simple Python Program - unsure with Range and loop

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

Upvotes: 0

Views: 172

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1124748

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))

Upvotes: 3

Ihor Pomaranskyy
Ihor Pomaranskyy

Reputation: 5651

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))

Upvotes: -1

FBidu
FBidu

Reputation: 1012

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

Upvotes: 1

Related Questions