python中while true输入正数_python入门学习------while和输入内容

1.input函数

例子1:

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

print("hello,"+name)

输出结果:

please enter your name:apple

hello,apple

例子2:直接在某个字符串后输入

prompt="I am glad to meet you."

prompt+="\nHi,I am glad to meet you,"

word=input(prompt)

print("hello,"+word)

输出结果:

I am glad to meet you.

Hi,I am glad to meet you,jack

hello,jack

2.当使用input函数时,如果我们输入的是数字,系统会把这个数字当成字符,而不能用于数值型的相关操作,这时我们需要使用int(),将输入看成是数值型变量

例子:

age=input("how old are you:")

age=int(age)

age>=18

输出结果:

True

3.      求模运算符%

4%3

计算结果:1

7%3

计算结果:1

运算符最后得到的值是整除后得到的余数

4.while循环

number=1

while number<=5:

print(number)

number+=1

输出结果:

1

2

3

4

5

5.用户选择何时退出

prompt="enter your message:"

message=""

while message!='quit':

message=input(prompt)

print(message)

输出结果:

enter your message:hello

hello

enter your message:lllll

lllll

enter your message:quit

quit

6. while循环中的break和continue

break直接退出,continue是继续执行但是会返回到循环的开头

例子:

while True:

city=input()

if city == "quit":

break

else:

print(city)

输出结果:

qq

qq

dg

dg

quit

例子number=0

while number<10:

number+=1

if number%2==0:

continue

else:

print(number)

输出结果:

1

3

5

7

9

7.使用用户输入来填充字典

responses={}

active=True

while active:

name=input("name ")

response=input("mountains ")

responses[name]=response

repeat=input("again:")

if repeat=="no":

active=False

print(responses)

for name,response in responses.items():

print(name+" "+response)

输出结果:

name ad

mountains sfd

again:y

name hshd

mountains ahf

again:no

{'ad': 'sfd', 'hshd': 'ahf'}

ad sfd

hshd ahf

总结:

while  break continue

字典的相关用法

添加字典内容,等(第六章)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值