Python:声明为整数和字符

时间:2017-02-26 01:57:55

标签: python declare

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    // allow preflight
    if (req.method === 'OPTIONS') {
        res.send(200);
    } else {
        next();
    }
});

当我执行此代码并输入" 10"我在shell中得到了内置函数chr。我希望它根据分数打印A或其他角色。例如,如果输入分数是8或9,则必须阅读B.但是,我试图先通过第一步。我是编程的新手,如果我能指出正确的方向,那将会有很大的帮助。

2 个答案:

答案 0 :(得分:0)

# declare score as integer
score = int

# declare rating as character
rating = chr

在两个语句之上,指定函数intchr,而不是使用默认值声明变量。 (顺便说一下,chr不是一个类型,而是一个将代码点值转换为字符的函数)

请改为:

score = 0    # or   int()
rating = ''  # or   'C'   # if you want C to be default rating

注意 score无需初始化,因为它是由score = input("Enter score: ")

分配的

答案 1 :(得分:0)

在python中,您不能进行静态类型化(即,您不能将变量固定为类型)。 Python是动态类型。

您需要的是将类型强制为输入变量。

# declare score as integer
score = '0' # the default score

# declare rating as character
rating = 'D' # default rating

# write "Enter score: "
# input score
score = input("Enter score: ")

# here, we are going to force convert score to integer
try:
    score = int (score)
except:
    print ('score is not convertable to integer')

# if score == 10 Then
#   set rating = "A"
# endif
if score == 10:
    rating = "A"

print(rating)