简单 Python 程序 - 不确定范围和循环
Simple Python Program - unsure with Range and loop
这是我应该在 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
你的线路
for NoOfGamesPlayed != NoOfGamesInMatch:
无效 Python。如果你想在这里使用循环,for
很有帮助,但你需要添加一个 range()
函数:
for NoOfGamesPlayed in range(int(NoOfGamesInMatch)):
参见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。
除了在 input()
行中使用小写字母 y
之外,您的代码与其他伪代码非常匹配;你可能想要更正它,因为你只在结果中测试 uppercase 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))
如果我猜到了你的意图,代码应该是这样的:
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))
在每种语言中,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
。在此处查看更多内容:
这是我应该在 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
你的线路
for NoOfGamesPlayed != NoOfGamesInMatch:
无效 Python。如果你想在这里使用循环,for
很有帮助,但你需要添加一个 range()
函数:
for NoOfGamesPlayed in range(int(NoOfGamesInMatch)):
参见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。
除了在 input()
行中使用小写字母 y
之外,您的代码与其他伪代码非常匹配;你可能想要更正它,因为你只在结果中测试 uppercase 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))
如果我猜到了你的意图,代码应该是这样的:
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))
在每种语言中,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
。在此处查看更多内容: