[英]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.