生成一个在1到100之间的随机数,且每次不改变

2 投票
2 回答
3839 浏览
提问于 2025-04-30 17:50

我正在用Python在谷歌应用引擎上创建一个小猜数字游戏,我想把答案设置为1到100之间的随机数字。

这可以通过以下方式实现...

import random

#Sets the answer to a random integer between 1 and 100
answer = random.randint(1,100)

这没问题。但是我有以下代码,每次用户猜测答案时,答案都会重置为一个随机数字,所以我在想,怎么才能让这个数字只存储一次,然后保持不变呢。

class GuessGame(webapp2.RequestHandler):
    def get(self):
        #Sets user to the current user
        user = users.get_current_user()
        #Create logout URL for user
        logout_url = users.create_logout_url(self.request.path)
        #If user is logged in do the following
        if user:
            #Tells jinja2 to use numberguess.html template
            template = JINJA_ENVIRONMENT.get_template('numberguess.html')
            #Sets the values to pass through to the template
            template_values = {
                'user': user.nickname(),
                'url_logout': logout_url,
                'url_logout_text': 'Log out',
            }
            #Sends those values to the template
            self.response.write(template.render(template_values))
        else:
            #If user isn't logged in then create the login url
            self.redirect(users.create_login_url(self.request.uri))

    def post(self):
        #Sets user to the current user
        user = users.get_current_user()
        #Create logout URL for user
        logout_url = users.create_logout_url(self.request.path)
        #If user is logged in do the following
        if user:
            #Tells jinja2 to use numberguess.html template
            template = JINJA_ENVIRONMENT.get_template('numberguess.html')
            #Gets the content from the form that has been sent via POST
            stguess = self.request.get('content')
            #Sets msg to a string
            msg = ''
            #Sets guess to minus 1 to begin with
            guess = -1
            try:
                #Sets guess to what was sent in the form only if its an int
                guess = int(stguess)
            except:
                #If not an int then set it to minus 1
                guess = -1
            #Sets the answer to a random integer between 1 and 100
            answer = random.randint(1,100)
            #If the guess is the same as the answer then set msg to correct
            if guess == answer:
                msg = 'Congrats! The guess is correct!'
            #If guess is less than 0 tell the user to provide positive number
            #This also works if user tries to input a string because of the try/except code
            elif guess < 0:
                msg = 'Please provide a positive number'
            #If guess too low then set msg
            elif guess < answer:
                msg = 'Your guess is too low'
            #If guess too high then set msg
            else:
                msg = 'Your guess is too high'
            #Sets the values to pass through to the template
            template_values = {
                'user': user.nickname(),
                'url_logout': logout_url,
                'url_logout_text': 'Log Out',
                'guess': stguess,
                'guess_text': msg,
            }
            #Sends those values to the template
            self.response.write(template.render(template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))
            self.redirect(users.create_login_url(self.request.uri))
暂无标签

2 个回答

-1

在生成随机数之前,先提供一个固定的种子。这样每次生成的数字序列都会是一样的。

 import random
 random.seed(5)
 random.randint(1,100)
=> 42
 random.randint(1,100)
=> 28
 random.randint(1,100)
=> 25
 random.randint(1,100)
=> 1
 random.seed(5)
 random.randint(1,100)
=> 42
 random.randint(1,100)
=> 28
 random.randint(1,100)
=> 25
 random.randint(1,100)
=> 1
2

你需要把这个值存储到数据存储里。

这可能需要一些调整,但我会这样做:

class StoredRandomNumber(ndb.Model):
    User = ndb.StringProperty()
    answer = ndb.IntegerProperty()
    GuessNumber = ndb.IntegerProperty(default=0)

在你的帖子里:

        randomEntity = StoredRandomNumber.query(StoredRandomNumber.User==str(user)).get()
        if randomEntity == None:
            randomEntity = StoredRandomNumber(User=str(user), answer=random.randint(1,100))


        if guess == randomEntity.answer:
            msg = 'Congrats! The guess is correct!'
            #re-seed for new game
            randomEntity.answer =random.randint(1,100)
            randomEntity.GuessNumber = 0
        elif guess < 0:
            msg = 'Please provide a positive number'
            randomEntity.GuessNumber +=1
        elif guess < randomEntity.answer:
            msg = 'Your guess is too low'
            randomEntity.GuessNumber +=1
        else:
            msg = 'Your guess is too high'
            randomEntity.GuessNumber +=1

        #Save the updates
        randomEntity.put()

撰写回答