python学习-用户输入和while循环

脑袋晕晕的,感觉状态很差,今天while循环这届节的基础知识还没看完,明天接着看! 

#11.1练习
#用户输入  input(x),打印出x之后等待用户输入一个值
message=input("tell me something,and I will repeat it back to you:")
print(message)    #在控制台出输入一个值,按enter打印
name=input("please enter your name:")
print(f"\nHello,{name}!")    #例如输出“Hello,xiaoma!”
name=input("xiaoma hhahah:")
print("xiaoma"+name+"nihao")  #两种方式输入“变量+普通语言”
print(f"xiaoma{name}hahah")
#当有多行需要提示的时候,可将提示值赋给一个变量,再将该变量传递给函数input()。
prompt = "If you tell us who you are,we can personalize the messages you see!"
prompt += "hahah"      #用运算符”+=“在前面赋值的变量prompt的字符串末尾附加一个字符串
prompt += "\nhahah"      #这种加了转义符“\n”,另起一行附加
name = input(prompt)
print(f"hello,{name}")

age=input("How old are you!")

#用int()来获取数值输入
age = input("How old are you!")
age = int(age)     #此处int()将数的字符串表示转换为数值表示

height = input('How old are you!')   #此处提示输入的是字符串
height = int(height)   #此处int()将数的字符串表示转换为数值表示
if height >= 48:
    print("\nYou are tall enough to ride!")
else:
    print("\nYou will be able to ride when you are a little older")

#求模运算符(%)
#求模运算符将两个数相处并返回余数,课用来判断一个数是奇数还是偶数
print(4%3)   #4/3余1
print(100%5)

Word=input("please put a number!")
Word=int(Word)
if Word%2==0:
    print(45)
else:
    print(100)
#P104小练习
word =input ("Dear customer, what car do you want")
print(f'Let me see if I can find you a {word}!')

word=input("How many people have ordered")
word=int(word)
if word>=8:
    print("meiyou kongwei")
else:
    print("youkongwei")

word = input("Please enter a number")
word = int(word)
if word%10==0:
    print("Yes")
else:
    print("No")

#while循环
number=1
while number<=5:   #只要number小于等于5,就一直以number+=1一直运行
    print(number)
    number+=1      #一旦number大于5,就停止运行
word="Have you eaten today?"
word+="\nIf you haven't eaten, have some with me!"
message=""
while message=="Yes":
    message=input(word)
    print(message)
prompt="Tell me something,and I will repeat it back to you!"
prompt+="\nEnter 'quit' to end the program"
message=""
while message!="quit":
    message=input(prompt)
    print(message)           #此处会一直循环,知道输入“quit”才停止
prompt="Tell me something,and I will repeat it back to you!"
prompt+="\nEnter 'quit' to end the program"
message=""
while message!="quit":
    message=input(prompt)
    if message=="quit":
        print(message)

####################
word=input("how are you,tell me a question:")
words=f"xiaoma is very {word}!"
print(words)
word="how are you,tell me a question."
word+="\nhow how how how is!"
name=input(word)
print(f"xiaoma is a boy {name}!")
print(9%2)
age=input("how are you!")
age=int(age)
age>=49
height=input("how ola are you!")
height=int(height)
if height>=180:
    print("my god!")
else:
    print("hahahaha")
number=input("please put in a number!")
number=int(number)
if number%4!=0:
    print("hahahhaha")
else:
    print("xiaoma hahaha")
number=1
while number<=100:
    print(number)
    number+=2

word="xiaoma ni zai nali?"
word+="\nxiaoma wo zai shan de duimian!"
message=[]
while message!="hahaha":
    message=input(word)
    print(word)
#使用标志
prompt="Tell me something,and I will repeat it back to you!"
prompt+="\nEnter 'quit' to end the program"
active=True      #将变量active设置为True,让程序最初处于活动状态,只要变量为True,程序就继续运行。
while active:
    message=input(prompt)
    if message=="quit":
        active=False
    else:
        print(message)
#使用break退出循环
prompt="please enter the name of a city you have visited!"
prompt+="\nEnter the 'quit' tou finish!"
while True:
    city=input(prompt)
    if city=="quit":
        break
    else:
        print("sorry!")
#在循环中使用continue
number=0
while number<=10:
    number+=1
    if number%2==0:     #此处if语句要缩进
        continue

    print(number)
number=12
while number<=1002:
    number+=6
    if number%3==1:
        continue

    print(number)
number=1
while number<=5:
    print(number)
    number+=1
number=1
while number<=100:
      print(number)
      number += 20
#P110小练习
pizza="Please enter some pizza ingredients"
pizza+="Please input quickly"
word=input(pizza)
while word:
    if word=="quit":
        print(word)
    else:
        print(f"We'll add {word} to the pizza!")
pizza="Please enter some pizza ingredients"
pizza+="\nPlease input quickly"
word=""
while word!="quit":
    word=input(pizza)
    print(f"We'll add {word} to the pizza")
pissa1 =['香蕉','vegetable','香肠','南瓜','辣椒']
message =input('请输入披萨的用料: ')
if message in pissa1:
   print('我们会在披萨中加入这种配料: '+ message)
elif  message =="quit":
   print('break')
else:
   print(message+'不属于披萨的原料')
word="Hey, handsome boy, how old are you?"
words=input(word)
if int(words)<3:
    print("free")
elif int(words)>=3 and int(words)<=12:
    print("The ticket is $10")
else:
    print("The ticket is $15")

while True:
    print("Enter 'quit' when you are finished.")
    age = input("请输入你的年龄:")
    if age == 'quit':
        break
    elif int(age) < 3:
        print("The price of you is free.")
    elif 3 <= int(age) <12:
        print("The price of you is $10.")
    elif 12 <= int(age) :
        print("The price of you is $15.")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值