>>> x= 4
>>> if x % 2 == 0:
print(x)
print('x is even number.')
4
x is even number.
>>> if x % 2 == 1:
print(x)
print('x is odd number')
>>>
>>> x = 4
>>> if x > 3:
print(x, 'x is more than 3')
elif x < 3 and x > 0:
print(x, 'x is less than 3')
else:
print(x, 'x is less than 0')
4 x is more than 3
>>>
题目:
编写程序,判断用户输入的数字是奇数还是偶数。
程序代码
'''
编写程序,判断用户输入的数字是奇数还是偶数。
'''
x = input("请输入一个自然数:")
if x.isdigit():
x = int(x)
if x % 2 == 0:
print("您输入的数字" + str(x) + "为偶数")
else:
print("您输入的数字{0}为奇数".format(x))
else:
print("请输入一个自然数")
运行结果
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_example01.py"
请输入一个自然数:1.1
请输入一个自然数
Process finished with exit code 0
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_example01.py"
请输入一个自然数:2
您输入的数字2为偶数
Process finished with exit code 0
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_example01.py"
请输入一个自然数:1
您输入的数字1为奇数
Process finished with exit code 0
涉及知识
isdigit()函数,判断输入的字符串是否为全数字(阿拉伯数字0~9)组成,该字符串不包括小数组成的字符串
>>> help(str.isdigit)
Help on method_descriptor:
isdigit(self, /)
Return True if the string is a digit string, False otherwise.
A string is a digit string if all characters in the string are digits and there
is at least one character in the string.
>>>
题目:
编写程序,判断用户输入的网站主域名是否符合规定格式要求
——网站主域名格式:www.xxxx.xxx
程序代码:
'''
编写程序,判断用户输入的网站主域名是否符合规定格式要求
——网站主域名格式:www.xxxx.xxx
'''
domain = input("请输入一个网站域名(格式为www.xxxx.xxx):")
postfix = ('com', 'net', 'cn')
# 以”.“作为分隔符将域名分割为列表
lst = domain.split(".")
if (len(lst) <2) or (len(lst) > 4):
print("Sorry!,Your domain is not right.")
elif lst[-1] not in postfix:
print("The domain does not comply with the regulations.")
else:
print("The domain is right.")
运行结果:
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_example02.py"
请输入一个网站域名(格式为www.xxxx.xxx):www.baidu.com
The domain is right.
Process finished with exit code 0
题目:
判断用户的键盘输入内容:
——如果都是数字,则将该数字扩大十倍,然后打印显示。
——如果是字母,则在其后面增加”@python“后打印显示。
——其他情况则将输入的内容按原样显示
程序代码:
"""
判断用户的键盘输入内容:
——如果都是数字,则将该数字扩大十倍,然后打印显示。
——如果是字母,则在其后面增加”@python“后打印显示。
——其他情况则将输入的内容按原样显示
"""
str1 = input("please enter any text:")
if str1.isdigit():
x = int(str1)
x = x * 10
print(x)
elif str1.isalpha():
print(str1 + "@python")
else:
print(str1)
运行结果:
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_job.py"
please enter any text:12
120
Process finished with exit code 0
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_job.py"
please enter any text:str
str@python
Process finished with exit code 0
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/1.3.2_job.py"
please enter any text:hello 12
hello 12
Process finished with exit code 0
涉及知识:
isalpha()函数,判断输入的文本是否全为字母
>>> help(str.isalpha)
Help on method_descriptor:
isalpha(self, /)
Return True if the string is an alphabetic string, False otherwise.
A string is alphabetic if all characters in the string are alphabetic and there
is at least one character in the string.
>>>