英文:
A Python game in which both a user and a computer randomly chooses a number from 1 - 10 and if the numbers are the same, the user wins else, he loses
问题
import random
class Game():
def __init__(self, computer_choice, user_choice):
self.computer_choice = computer_choice
self.user_choice = user_choice
def computer(self):
self.computer_choice = random.randint(0, 10)
def user(self):
self.user_choice = int(input("输入一个从0到10的随机数字:"))
def decision(self):
if self.user_choice >= 0 and self.user_choice <= 10:
if self.computer_choice == self.user_choice:
print("你赢了!")
else:
print("你输了!")
else:
print("你输入了一个无效的选项")
def main():
game = Game(0, 0)
game.computer()
game.user()
game.decision()
if __name__ == "__main__":
main()
英文:
Write a game in Python so that a user will randomly choose a number from 0 to 10 and the computer will also do same. If the numbers are equal, you win else you lose.
This is what I wrote. When I run, nothing happens. Can anyone rewrite the full game for me?
import random
class Game():
def __init__(self, computer_choice, user_choice):
self.computer_choice = computer_choice
self.user_choice = user_choice
def computer(self):
self.computer_choice = random.randint(range(1,10))
def user(self):
self.user_choice= int(input("Enter a random number from 1 to 10: "))
def decision(self):
if self.user_choice == int(range(1,10)):
if self.computer_choice == self.user_choice:
print("You won!")
else:
print("You lost!")
else:
print("You entered an invalid option")
def main():
if __name__ == "__main__":
main()
</details>
# 答案1
**得分**: 0
很难确定,因为我们无法看到`main`中的内容,但问题很可能出在这一行:
```python
if self.user_choice == int(range(1, 10)):
您试图比较用户的选择,一个介于1到10之间的整数,与一系列整数,即一个范围对象。在最佳情况下,这不会起作用。在这种情况下,将范围转换为整数应该会引发TypeError
异常。相反,您可以这样写:
if self.user_choice in range(1, 11):
或者更好的写法是:
if 1 <= self.user_choice <= 10:
英文:
It is hard to know for sure since we can't see what is in main
, but the problem could very well be this line:
if self.user_choice == int(range(1,10)):
You are attempting to compare the user's choice, an integer between 1 and 10, with a range of integers, a range object. Under the best of circumstances that would not work. In this case, you casting the range to an int should result in a TypeError
exception. Instead, you could write:
if self.user_choice in range(1, 11):
or better yet:
if 1 <= self.user_choice <= 10:
答案2
得分: 0
注意:主要问题已在rhurwitz的回答中得到解决,但还有一个补充:
在main
函数中,您陷入了一个无限循环:我还建议这样做:
def main():
if __name__ == "__main__":
game = Game(0, 0)
game.computer()
game.user()
game.decision()
总之:
import random
class Game():
def __init__(self):
self.computer_choice = random.randint(1,10)
self.user_choice = int(input("输入一个从1到10的随机数字:"))
def decision(self):
if self.user_choice in list(range(11)):
if self.computer_choice == self.user_choice:
print("你赢了!")
else:
print("你输了!")
else:
print("您输入了无效选项")
def main():
game = Game()
game.decision()
main()
英文:
Note: Main problem is fixed in rhurwitz's answer, but there is an addition:
You are entering an infinite loop in the main
function: I'd also recommend doing:
def main():
if __name__ == "__main__":
game = Game(0, 0)
game.computer()
game.user()
game.decision()
In all:
import random
class Game():
def __init__(self):
self.computer_choice = random.randint(1,10)
self.user_choice = int(input("Enter a random number from 1 to 10: "))
def decision(self):
if self.user_choice in list(range(11)):
if self.computer_choice == self.user_choice:
print("You won!")
else:
print("You lost!")
else:
print("You entered an invalid option")
def main():
game = Game()
game.decision()
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论