简单的Python程序 - 不确定Range和循环

时间:2015-05-30 15:10:51

标签: python for-loop while-loop range

这是我应该在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

这是我在python中创建的,它不起作用,我不明白为什么?

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

我尝试了in range部分,当我输入一个程序输入了多少游戏时,我收到了这个错误。

    for NoOfGamesPlayed in range(NoOfGamesInMatch):
TypeError: 'str' object cannot be interpreted as an integer

3 个答案:

答案 0 :(得分:3)

你的行

for NoOfGamesPlayed != NoOfGamesInMatch:  

不是有效的Python。如果您想在此处使用循环,for会很有帮助,但您需要添加range()函数:

for NoOfGamesPlayed in range(int(NoOfGamesInMatch)):

请参阅Python tutorial on the for construct。由于input()函数返回字符串,因此您需要先使用int() function将其转换为整数。

除了在y行中使用小写input()之外,您的代码在其他方面与伪代码非常匹配;您可能想要更正,因为您只在结果中测试大写 Y

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

你在PlayerTwoScore更新中也犯了一个小错字;将第二个=替换为+

PlayerTwoScore = PlayerTwoScore + 1

将它们放在一起可以:

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

答案 1 :(得分:1)

在每种语言中,for循环更常用于迭代一系列值,如

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

另一方面,while循环用于执行代码块,而给定条件的计算结果为真

while i_am_hunger: eat()
while list_is_empty
while list_is_not_empty

等等。

在我看来,您的案例更适合while循环。类似的东西:

while NoOfGamesPlayed != NoOfGamesInMatch:  *do something*

最后说明: Python有一些样式指南,旨在使您的代码更清晰。虽然风格有时是个人选择,但如果你花点时间阅读它们会很好。例如,在您的情况下,您的变量名称应按no_of_games_played中的下划线划分。在这里结帐更多:

Google Style Guide

PEP-8

答案 2 :(得分:-1)

如果我猜到了你的意图,代码应如下所示:

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