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评论68阅读模式
英文:

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(&quot;Enter a random number from 1 to 10: &quot;))  

    def decision(self): 
        if self.user_choice == int(range(1,10)): 
         
            if self.computer_choice == self.user_choice:
                print(&quot;You won!&quot;)
            else:
                print(&quot;You lost!&quot;)    
        else:
            print(&quot;You entered an invalid option&quot;)

def main():
    
    if __name__ == &quot;__main__&quot;:
        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 &lt;= self.user_choice &lt;= 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__ == &quot;__main__&quot;:
       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(&quot;Enter a random number from 1 to 10: &quot;))

    def decision(self): 
        if self.user_choice in list(range(11)): 
         
            if self.computer_choice == self.user_choice:
                print(&quot;You won!&quot;)
            else:
                print(&quot;You lost!&quot;)    
        else:
            print(&quot;You entered an invalid option&quot;)

def main():
    game = Game()
    game.decision()

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:

确定