Python:声明为整数和字符

标签 python declare

# declare score as integer
score = int

# declare rating as character
rating = chr

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

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

print(rating)

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

最佳答案

# 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: ") 分配的

关于Python:声明为整数和字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54282337/

相关文章:

python - BrokenProcessPool 关于在 cross_val_score 中使用 n_jobs 参数

python - Urllib 算作网页点击吗?

python - 使用 matplotlib 在 python 中设置 ylim

java - eclipse "can not be resolved to variable"

python - 在 Python 中的函数外部声明全局变量

sql - 如何让变量在 Oracle SQL Developer 中真正起作用?

Python - 在列表列表中查找元组

python - 登录 selenium 测试 django 所有身份验证失败

c++ - 'struct random_data* buf' 的类型是什么?

Bash:如何使用包含方括号或其他特殊字符的键来保留和恢复关联数组