简单用户输入
Posted python-beginner
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单用户输入相关的知识,希望对你有一定的参考价值。
death_age = 80
name = input(“your name:”)
age = input("your age:")
#print(name,age)
print("your name:",name)
print("you can still live for ",(death_age - age))
或者print("you can still live for "+(death_age - age))
直接运行报错
syntax error :unsupported operand type
语法错误:不支持的操作运算类型
原因分析:input默认接收数据类型为str(string),str跟int类型不能进行数学运算,需进行强制数据类型转换
猜年龄游戏
age_of_ principal = 56
guess_age = int (input(">>:"))
if guess_age == age_of_principal:
print("yes,you got it")
else;
print("no,you are wrong")
缩进介绍
为什么要有缩进?程序需要知道代码什么时候开始、什么时候结束。python通过统一的缩进规范同级代码。
age_of_ principal = 56
guess_age = int (input(">>:"))
if guess_age == age_of_principal:
print("yes,you got it")
else;
print("no,you are wrong")
IndentationError:expected an indented block
猜年龄增加大小判断
age_of_principal = 56
guess_age = int(input(">>:"))
if guess_age == age_of_principal:
print("yes,you got it")
elif guess_age > age_of_principal:
print("should try smaller...")
else:
print("should try bigger...")
score = int(input("scores:"))
if score > 90:
print("A")
elif score > 80:
print("B")
elif score > 70:
print("C")
else:
print("D")
以上是关于简单用户输入的主要内容,如果未能解决你的问题,请参考以下文章