反向字符串?

时间:2014-10-31 02:28:11

标签: python

我已经被困在这个任务上好几个小时了,当我发现Python代码来反转我的stguess(这是一个存储来自HTML文件的用户输入的变量)时,我在错误后得到错误。其中一些几乎说明它不理解像stguess [:: - 1] stguess.reversed()那样的反向方法它只是不起作用。另外我有一个愚蠢的“如果!= 1”我实际上应该找到一种方法来确保只能输入az和A-Z但是我又不知道怎么做了所以我刚刚输入了所以我知道我可以传递价值并试图扭转它。好吧:

if guess != 1:
            print''.join(reversed(stguess)) //But this does not reverse the string?

import webapp2
import os
import jinja2
import logging
from google.appengine.api import users


JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')))
extentions=['jinja2.ext.autoescape']
user = users.get_current_user()


class reverse(webapp2.RequestHandler):
    def get(self):
    user = users.get_current_user()
    logout_url = users.create_login_url(self.request.path)
    template = JINJA_ENVIRONMENT.get_template('reverseword.html')
    stguess = self.request.get('content')
    msg = ''
    template_values = {
        'user': user.nickname(),
        'url_logout': logout_url,
        'url_logout_text': 'Log Out',
        'guess': stguess,
        'guess_text': msg,
        }
    if user:
        stguess = self.request.get('content')
        self.response.write(template.render(template_values))
    else:
        self.redirect(users.create_login_url(self.request.url))

def reverse(text):
    if len(stguess) <= 1:
        return stguess

class reversepost(webapp2.RequestHandler):

def get(self):
    user = users.get_current_user()
    logout_url = users.create_login_url(self.request.path)
    template = JINJA_ENVIRONMENT.get_template('reverseword.html')
    stguess = self.request.get('content')
    msg = ''
    template_values = {
        'user': user.nickname(),
        'url_logout': logout_url,
        'url_logout_text': 'Log Out',
        'guess': stguess,
        'guess_text': msg,
        }
    if user:
        stguess = self.request.get('content')
        self.response.write(template.render(template_values))
    else:
        self.redirect(users.create_login_url(self.request.url))

def post(self):
    logging.info("post")
    logout_url = users.create_login_url(self.request.path)
    template = JINJA_ENVIRONMENT.get_template('reverseword.html')
    stguess = self.request.get('content')

    if user:
        logging.info("user found")
        msg = ''
        guess = -1
        try:
            guess = str(stguess)
            logging.info("guess contains "+str(guess))
        except:
            guess = -1
        **if guess != 1: //if guess is not equal to 1
            return stguess.reverse()** //return user input.reversed()
        else:
            msg = 'Didnt work'
        template_values = {
        'user': user.nickname(),
        'url_logout': logout_url,
        'url_logout_text': 'Log Out',
        'guess': guess,
        'guess_text': msg,
        }
        self.response.write(template.render(template_values))
    else:
        self.redirect(users.create_login_url(self.request.url))

app = webapp2.WSGIApplication([     ('/',反向) ],debug = True)

1 个答案:

答案 0 :(得分:0)

print ''.join(reversed(stguess))

只会打印字符串的反转版本。

如果您想存储该值,则需要将stguess重新绑定到反转值。即

stguess = stguess[::-1]
print stguess

stguess = ''.join(reversed(stguess))
print stguess