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

huangapple go评论105阅读模式
英文:

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

问题

  1. import random
  2. class Game():
  3. def __init__(self, computer_choice, user_choice):
  4. self.computer_choice = computer_choice
  5. self.user_choice = user_choice
  6. def computer(self):
  7. self.computer_choice = random.randint(0, 10)
  8. def user(self):
  9. self.user_choice = int(input("输入一个从0到10的随机数字:"))
  10. def decision(self):
  11. if self.user_choice >= 0 and self.user_choice <= 10:
  12. if self.computer_choice == self.user_choice:
  13. print("你赢了!")
  14. else:
  15. print("你输了!")
  16. else:
  17. print("你输入了一个无效的选项")
  18. def main():
  19. game = Game(0, 0)
  20. game.computer()
  21. game.user()
  22. game.decision()
  23. if __name__ == "__main__":
  24. 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?

  1. import random
  2. class Game():
  3. def __init__(self, computer_choice, user_choice):
  4. self.computer_choice = computer_choice
  5. self.user_choice = user_choice
  6. def computer(self):
  7. self.computer_choice = random.randint(range(1,10))
  8. def user(self):
  9. self.user_choice= int(input(&quot;Enter a random number from 1 to 10: &quot;))
  10. def decision(self):
  11. if self.user_choice == int(range(1,10)):
  12. if self.computer_choice == self.user_choice:
  13. print(&quot;You won!&quot;)
  14. else:
  15. print(&quot;You lost!&quot;)
  16. else:
  17. print(&quot;You entered an invalid option&quot;)
  18. def main():
  19. if __name__ == &quot;__main__&quot;:
  20. main()
  21. </details>
  22. # 答案1
  23. **得分**: 0
  24. 很难确定因为我们无法看到`main`中的内容但问题很可能出在这一行
  25. ```python
  26. if self.user_choice == int(range(1, 10)):

您试图比较用户的选择,一个介于1到10之间的整数,与一系列整数,即一个范围对象。在最佳情况下,这不会起作用。在这种情况下,将范围转换为整数应该会引发TypeError异常。相反,您可以这样写:

  1. if self.user_choice in range(1, 11):

或者更好的写法是:

  1. 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:

  1. 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:

  1. if self.user_choice in range(1, 11):

or better yet:

  1. if 1 &lt;= self.user_choice &lt;= 10:

答案2

得分: 0

注意:主要问题已在rhurwitz的回答中得到解决,但还有一个补充:

main函数中,您陷入了一个无限循环:我还建议这样做:

  1. def main():
  2. if __name__ == "__main__":
  3. game = Game(0, 0)
  4. game.computer()
  5. game.user()
  6. game.decision()

总之:

  1. import random
  2. class Game():
  3. def __init__(self):
  4. self.computer_choice = random.randint(1,10)
  5. self.user_choice = int(input("输入一个从1到10的随机数字:"))
  6. def decision(self):
  7. if self.user_choice in list(range(11)):
  8. if self.computer_choice == self.user_choice:
  9. print("你赢了!")
  10. else:
  11. print("你输了!")
  12. else:
  13. print("您输入了无效选项")
  14. def main():
  15. game = Game()
  16. game.decision()
  17. 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:

  1. def main():
  2. if __name__ == &quot;__main__&quot;:
  3. game = Game(0, 0)
  4. game.computer()
  5. game.user()
  6. game.decision()

In all:

  1. import random
  2. class Game():
  3. def __init__(self):
  4. self.computer_choice = random.randint(1,10)
  5. self.user_choice = int(input(&quot;Enter a random number from 1 to 10: &quot;))
  6. def decision(self):
  7. if self.user_choice in list(range(11)):
  8. if self.computer_choice == self.user_choice:
  9. print(&quot;You won!&quot;)
  10. else:
  11. print(&quot;You lost!&quot;)
  12. else:
  13. print(&quot;You entered an invalid option&quot;)
  14. def main():
  15. game = Game()
  16. game.decision()
  17. main()

huangapple
  • 本文由 发表于 2023年1月9日 13:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定