最后打印第一个变量(index = 0)

时间:2019-04-04 23:18:01

标签: python

我试图在末尾附加逗号分隔的字符串的第一个元素。如果用户输入“第一,第二,第三”,则应返回[“第一”,“第二”,“第三”,“第一”]

所以我真的很接近完成它,但是我的代码先返回['first','second','third] first

我当时想先使用.append(),然后再打印代码。有人可以详细说明.append()吗?

#importing Flask
from flask import Flask

#creating an app instance in the current namespace
app = Flask(__name__)

#decorator
@app.route("/")
@app.route("/square/<int:side1>/<int:side2>/<int:side3>/<int:side4>")

def add(side1,side2,side3,side4):
    """
    perimeter of a square
    """
    perim = (side1 + side2 + side3 + side4)
    return "<h2>Your square's parimeter is {}</h2>".format(perim)

@app.route("/triangle/<int:b>/<int:h>")

def area_tr(b,h):
    """
    Function calculates the area of a triangle
    formula = (base * height)/2
    """
    area_tr = b * h / 2
    return "<h2>Your triangle's area is {}</h2>".format(area_tr)

@app.route("/cost/<int:u>/<int:p>")


def tc_wjacket(u,p):
    """
    total cost of buying raw materials for a winter jacket.
    Total cost = Number of units X price per unit.
    """
    total_cost = u * p
    return "<h2>Your triangle's area is {}</h2>".format(total_cost)


#debug=True allows us to make changes to our code and see them in the browser without having to re-run our app
app.run(debug=True, port=8000, host='0.0.0.0')

1 个答案:

答案 0 :(得分:0)

append()函数允许用户将值添加到列表的末尾。 因此,如果您的列表是["hi", 1, "hello"] 然后我们执行my_list.append(100),新列表就会出现 ["hi", 1, "hello", 100]

对于您的情况,您需要做的就是在执行操作时将值读入列表 拆分 然后

s = input('Please enter a series of comma-separated strings: ')
l = s.split(",")
l.append(l[0])
print(l)

您的列表现在将采用正确的格式。