python学了可以赚外快嘛_试图学Python赚外快的第7天

感觉吧,越来越多了,今天主要要学while循环的,本来打算一天一章,一本书20章的话很快就搞定了,我觉得还是有点天真了 前边还好,后边打的程序查错❌查半天,主要是汉子切换时忘了换回来,标点符号错的,还有缩进行 感觉把前面注意的通通都犯了一遍,有时候查好几次,查不出毛病,你就说气不气,程序员没头发我突然理解了 今天就开始学习吧,硬是留了点小尾巴课后题,图片就放在后边不在中间放了哦

第7章:While循环和用户输入

1、Input()让程序能够暂停,使得用户可以输入信息

如下例子:

message=input("Tell me something,and I will repeat it back to you: ")

print(message)

2、如果提示语太长可以使用+=使得信息分行转化,输入行只能在+=这一行

name=input("please enter your name: ")

print("Hello, "+name +"!")

prompt = "If you tell us who you are, we can personalize the messages you see."

prompt +="\nWhat is your first name? "

name=input(prompt)

print("\nHello, "+name+"!")

3、int()输入数值,不要和str()记混了哦,这个是我们第二章说过的,将非字符串显示为字符串

height=int(input("How tall are you,in inches?"))

#也可以在这一行写height=int(hight)

if height >=36:

print("\nYou're tall enough to ride!")

else:

print("\nYou will be able to ride when you are a little older.")

4、余数运算用%,比如5%4余数为1(求模运算)

下面是输入一个数显示为奇数或者偶数

number=int(input("Enter a number, and I'll tell you if it's even or odd:"))

if number%2==0:

print("\nThe number "+str(number)+" is even")

else:

print("\nThe number "+str(number)+" is odd")

5、使用while循环语句(嗯,while循环就是当条件不满足时停止,for循环是针对每个元素都要执行遍的)还有布尔表达,开启程序运行时可以使用哦,当然满足后要终止程序

#程序1

prompt="\nTell me something ,and I'll repeat it back to you:"

prompt+="\nEnter quit to end the program:"

message=""

while message!='quit':

message=input(prompt)

print(message) #会有一个quit打印出来

#程序1的改进,程序2

prompt="\nTell me something ,and I'll repeat it back to you:"

prompt+="\nEnter quit to end the program:"

message=""

while message!='quit':

message=input(prompt)

if message!='quit': #只有当message不等于quit才打印,当meesage等于quit时,就不会出现末尾的quit

print(message)

#程序2的改进,含标志(布尔表达式)

prompt="\nTell me something ,and I'll repeat it back to you:"

prompt+="\nEnter quit to end the program:"

active='True'

while active:

message=input(prompt)

if message=='quit':

active=False #当message不等于quit时,程序运行不下去,返回接着用户重新输入,直到message等于quit时运行打印

else:

print(message)

好了,就到这吧,溜了溜了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值