age = 19
if age >= 18:
print('You are old enough to vote!')
You are old enough to vote!
>>> car = 'bnw'
>>> car == 'bnw'
True
>>> car = 'Audi'
>>> car == 'bnw'
False
>>> car = 'Audi'
>>> car.lower() == 'adui' #暂时的,不覆盖
True
>>> car
>'Adui'
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
banned_users = ['andrew', 'carlina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ', you can post a response if you wish.')
Marie, you can post a response if you wish.
age = 17
if age >= 18:
print('You are old enough to vote!')
print('Have you registered to vote yet?')
else: #输出这个
print('Sorry,you are too young to vote.')
print('Please register to vote as soon as you turn 18!')
Sorry,you are too young to vote.
Please register to vote as soon as you turn 18!
age = 12
if age < 4:
print('Your admission cost in $0.')
elif age < 18: #输出这个
print('Your admission cost in $5.')
else:
print('Your admission cost in $10.')
Your admission cost in $5.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print('Your admission cost is $' + str(price) + '.')
Your admission cost is $5.
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print('Your admission cost is $' + str(price) + '.')
Your admission cost is $5.
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings: #添加
print('Adding mushrooms.')
if 'pepperoni' in requested_toppings: #没有这个,没加进去
print('Adding pepperoni.')
if 'extra cheese' in requested_toppings: #添加
print('Adding extra cheese.')
print('\nFinished making your pizza!')
Adding mushrooms.
Adding extra cheese.
Finished making your pizza!
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings: #遍历列表
print('Adding ' + requested_topping + '.')
print('\nFinished making your pizza!')
Adding mushrooms.
Adding green peppers.
Adding extra cheese.
Finished making your pizza!
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings: #遍历
if requested_topping == 'green peppers':
print('Sorry, we are out of green peppers right now')
else:
print('Adding ' + requested_topping + '.')
print('\nFinished making your pizza!')
Adding mushrooms.
Sorry, we are out of green peppers right now
Adding extra cheese.
Finished making your pizza!
requested_toppings = []
if requested_toppings: #默认非空执行if里面的
for requested_topping in requested_toppings:
print('Adding' + requested_topping + '.')
print('\nFinished making your pizza!')
else: #为空时,执行else
print('Are you sure you want a plain pizza?')
Are you sure you want a plain pizza?
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'fresh fries', 'extra cheese'] #中间的没有
for requested_topping in requested_toppings: #遍历需要的列表
if requested_topping in available_toppings: #如果需要的在现有的里面
print('Adding ' + requested_topping + '.')
else: #如果需要的不在现有的里面
print("Sorry, we don't have " + requested_topping + '.')
print('\nFinished making your pizza!')
Adding mushrooms.
Sorry, we don't have fresh fries.
Adding extra cheese.
Finished making your pizza!
只能输入数字,判断对错
temp=input('猜数字,请输入数字:') #输入的为字符型
guess=int(temp) #字符型转换整型
if guess==8: #英文冒号
print('Yes') #缩进
else:
print('No')
输入数字,判断对错,错的会告诉大了还是小了
temp=input('猜数字,请输入数字:')
guess=int(temp)
if guess==8:
print('Yes')
else: #else里面再嵌套一个if-else
if guess<8:
print('小了')
else: #大于8
print('大了')
print('结束了,不玩了')
能多次输入,判断对错,错的会告诉大了还是小了,直到对为止结束
temp=input('猜数字,请输入数字:')
guess=int(temp)
while guess !=8: #while循环,直到对为止
temp=input('猜错了,重新输入') #重新输入
guess = int(temp) #重新转化为整型
if guess==8:
print('Yes')
else:
if guess<8:
print('小了')
else:
print('大了')
print('结束了,不玩了')
示例:
import random #import用于调用
a = random.randint(0,2) #产生0到2的随机数,三种可能(0,1,2)
print(a)
import random #导入random模块,模块后缀为.py
secret = random.randint(1, 10) #引用randint函数
print(secret)
随机生成1到10的数
要猜的数字随机生成
import random #import调用random模块
secret = random.randint(1,10) #调用import模块中内置函数randint()生成随机数,随机数为1到10
print('开始游戏')
temp=input('猜数字,请输入数字:')
guess=int(temp)
while guess !=secret:
temp=input('猜错了,重新输入')
guess = int(temp)
if guess==secret:
print('Yes')
else:
if guess<secret:
print('小了')
else:
print('大了')
print('结束了,不玩了')
score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
print('A')
if 90 > score >= 80:
print('B')
if 80 > score >=60:
print('C')
if 60 > score >= 0:
print('0')
if score <0 or score > 100:
print('输入错误!')
score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
print('A')
else: #else里面细分其他情况
if 90 > score >= 80:
print('B')
else:
if 80 > score >=60:
print('C')
else:
if 60 > score >= 0:
print('0')
else:
print('输入错误!')
score = int(input('请输入一个分数:'))
if 100 >= score >= 90:
print('A')
elif 90 > score >= 80:
print('B')
elif 80 > score >=60:
print('C')
elif 60 > score >= 0:
print('0')
else:
print('输入错误!')
n = 0
while n < 5:
score = int(input('请输入一个分数:'))
if 100 >= score > 90:
print('A')
elif 90 >= score > 80:
print('B')
elif 80 >= score > 60:
print('C')
elif score<=60:
print('D')
else:
print('输入错误')
n = n+1 #记录循环次数,好跳出循环
E = input('请输入分数')
score = int(E)
while score <= 100:
E = input('请输入分数') #再一次输入
score = int(E) #转换为整型
if 100 >= score >= 90:
print('A')
elif 90 > score >= 80:
print('B')
elif 80 > score >= 60:
print('C')
elif score < 60:
print('D')
if score > 100: #与while对齐
print('输入错误')
print('结束')