用户输入和while 循环

input 工作原理

函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中。

message = input("need to  input string ")
print(message)

input() 接受一个参数作为提示,程序等待用户输入后,在用户回车确认后继续运行,输入存储在变量中

或者:

promt = "hello plse input you name"
promt += "\nyou fist name "

input(promt)

使用 int()来获取数值输入

使用函数input()时,Python将用户输入解读为字符串,将输入作为数字使用,使用函数init()

求模运算

处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数。
求模运算符不会指出一个数是另一个数的多少倍,而只指出余数是多少
如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。你可利用这一点来判断一个数是奇数还是偶数

number % 2 == 0:

while循环

current_number = 1
while current_number <= 5:
    print( str(current_number)  + "回合")
    current_number += 1
    print(current_number)

让用户选择何时退出

prompt = "\nTell 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) 

break 退出循环

立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break语句

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt)

active = True
while active  :
    xx = input(prompt)
    if xx == "quit" :
        active = False
    else :
        print(xx)
    

break循环

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
print(prompt)

while True  :
    xx = input(prompt)
    if xx == "quit" :
        break
    else :
        print(xx)

循环中使用continue

返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句,它不像break语句那样不再执行余下的代码并退出整个循环


    current_number = 0
while current_number < 10:
     current_number += 1
     if current_number % 2 == 0:
             continue
     print(current_number) 

首先将current_number设置成了0,由于它小于10,Python进入while循环。进入循环后,我们以步长1的方式往上数 ,因此current_number为1。接下来,if语句检查current_number与2的求模运算结果。如果结果为0(意味着current_number可被2整除),就执行continue语句,
让Python忽略余下的代码,并返回到循环的开头。如果当前的数字不能被2整除,就执行循环中余下的代码,Python将这个数字打印出来

避免无限循环

原文地址:https://www.cnblogs.com/g2thend/p/11741452.html

时间: 2024-08-29 04:28:26

用户输入和while 循环的相关文章

用户输入与while循环

函数input()的工作原理: 函数input()让程序短暂运行,等待用户输入一些文本,获取用户输入后将其存储在一个变量中 测试input()功能-- #!/usr/bin/env python#filename:input().py message=input("tell me something and, I will repeat back to you: ")print(message) 效果: [[email protected] Day3]# ./input.py tell

Python学习笔记6_用户输入和while循环

1.1   函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用.例如,下面的程序让用户输入一些文本,再将这些文本呈现给用户: message = input("Tell me something, and I will repeat it back to you: ") #程序等待用户输入,并在用户按回车键后继续运行 print(message) 1.1.1  编写清晰的程序 每当你使用函

【Python初级-7】用户输入和while循环

1.函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,方便使用. message = input("Please input your name here: ") print(message) 输出: [[email protected] python-test]# ./test_python.py Please input your name here: Alex Alex 函数input()接收一个参数:即要向用户显示的提示或

用户输入和while循环

函数input()可以让程序暂停执行,等待用户输入后,按回车继续执行. message = input("请输入:") print(message)   通常输入的东西程序会默认其为字符串,若想转化为数字,需要加int函数. while循环 a=1 while a <=5: break 退出当前循环 continue 返回循环头,不执行后续语句. python 传递任意数量的实参: def make_cokie(*source) print(source) make_cokie(

Class - 7 用户输入和while循环

7.1 函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,python将其存储在一个变量中,以方便使用.例: message = input("Tell me somethhing, and i will repeat it back to you:") print(message) 输出: Tell me somethhing, and i will repeat it back to you:Hello! Hello! 函数inp

python的用户输入和while循环

1.函数input()工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. (1)获取数值可以用 int()函数 (2)求模运算符(%)将两个数相除并返回余数 2.while循环简介 (1)for循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条件不满足为止. 例子 while  条件: 代码块 (2)使用标志 在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否

《python编程从入门到实践》用户输入和while循环

input() input向用户显示提示,接受用户输入,像是把C中的printf和scanf结合了起来,不用像C那样提示还得单独写一行 1 age = input("how old are you?") int() 将用户输入转换为数值 1 age = input("how old are you?") 2 age = int(age)#将数字的字符型转变为数值 3 if age > 18: 4 print("adult") 5 输入 19

第七章-用户输入和while循环

1.函数input()的工作原理 函数input()让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. 1.编写清晰程序 每当你使用函数input()时,都应指定清晰而易于明白的提示,通过在提示末尾(这里是冒号后面)包含一个空格,可将提示与用户输入分开,让用户清楚地知道其输入始于何处. 2.使用int()来获取数值输入 使用函数input()时,Python将用户输入解读为字符串. 将数值输入用于计算和比较前,务必将其转化为数值表示. 3.求模

第7章 用户输入和while循环

input()函数input()让程序暂停使用,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便后续使用 message = input("Tell me something, and I will repeat it back to you: ") print(message) 输出Tell me something, and I will repeat it back to you: Hello everyone!Hello everyone! hei