简体   繁体   English

创建一个介于1到100之间且不会每次更改的随机数

[英]Creating a random number between 1-100 that doesn't change everytime

Im creating a little guess game in python google app engine and im trying to set the answer to be a random number between 1-100 我在python Google App Engine中创建了一个小猜谜游戏,我试图将答案设置为1-100之间的随机数

which can be done by... 可以通过...

import random

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

Which is fine. 没关系 But I have the following code and every time the user guesses the answer it will be reset to a random number so i'm wondering which is the best way to have the number stored only once and then it will remain as that number. 但是我有以下代码,并且每当用户猜测答案时,它将被重置为一个随机数,因此我想知道哪种方法是将数字仅存储一次的最佳方法,然后将其保留为该数字。

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))

You need to store the value in the datastore . 您需要将值存储数据存储区中

This may need some polish but here is how I would do it: 这可能需要一些抛光,但是这是我的处理方法:

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

in your post: 在您的帖子中:

        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()

Provide a fixed seed before generating the random number. 在生成随机数之前,请提供固定的种子。 That'll cause the same sequence of numbers to be generated every time. 这将导致每次生成相同的数字序列。

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指出1-100的数字是否可以被3或4整除 - Stating if a number from 1-100 is divisible by 3 or 4 Numpy - 从范围 (1-100) 创建一个随机数组 - Numpy - create a random array from the range (1-100) Python 程序:生成 1 到 100 之间的随机数 - Python program that: Generates a random number between 1 and 100 一个 liner 生成一个 0 到 100 之间的随机数,但最后一位数字以 0、1、2、3、4 结尾 - one liner to generate a random number between 0 and 100 but last digit ends in 0, 1, 2, 3, 4 Python:使用while循环1-100偶数并在1行上排序3个数字 - Python : Using while loop 1-100 even number and sort 3 number on 1 line 如何在 Python 中将用户输入限制为仅某些整数(即:仅在 1-100 之间,其中还包括 1 和 100) - How can I limit the user input to only certain integers (ie: Only between 1-100, which also includes 1 and 100) in Python 在 python 的循环中每次生成一个随机数 - generating a random number everytime in a loop in python 我正在尝试创建一个程序,其中计算机在 1 到 100 之间选择一个随机数并尝试猜测它选择的数字 - I am trying to create a program in which the computer picks a random number between 1 and 100 and tries to guess the number it picked 在Python 3中将1-100的数字作为单词打印 - Printing numbers from 1-100 as words in Python 3 在字符串中创建一个随机数 - Creating a random number in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM