随机函数;if-else和while

# 随机数的生成
import random

die1 = random.randint(1,6)
die2 = random.randrange(6) + 1

total = die1 + die2

print("You rolled a %d and a %d for a total of %d"%(die1,die2,total))


You rolled a 5 and a 4 for a total of 9
# if 语句
print("Welcome to System Security Inc.")
print("--where security is our middle name\n")

password = input("Enter your password:")

if password == "lzq295406832":
    print("Acess Granted")
  
input("\nPress the enter key to exit.")
Welcome to System Security Inc.
--where security is our middle name

Enter your password:zlq

Press the enter key to exit.





''
# if else语句
print("Welcome to System Security Inc.")
print("--where security is our middle name\n")

password = input("Enter your password:")

if password == "lzq295406832":
    print("Access Granted")
else:
    print("Access Denied")

Welcome to System Security Inc.
--where security is our middle name

Enter your password:lzq
Access Denied
#elif 语句
import random

print("I sense your energy.Your true emotions are coming across my screen.")
print("You are...")

mood = random.randint(1,3)

if mood == 1:
    print("exciting")
elif mood == 2:
    print("normal")
else:
    print("sad")
I sense your energy.Your true emotions are coming across my screen.
You are...
exciting
#while循环
print("\tWelcome to the 'Three-Year-Old Simulator'\n")
print("This program simulates a conversation with a three-year-old child.")
print("Try to stop the madness.\n")

response = ""
while response != "Because.":
    response = input("Why?\n")
    
print("Oh,Okay.")
	Welcome to the 'Three-Year-Old Simulator'

This program simulates a conversation with a three-year-old child.
Try to stop the madness.

Why?
because
Why?
Because
Why?
Because.
Oh,Okay.

在这里插入图片描述

#将值作为条件
print("Welcome to the Chateau D' Food")
print("It seems we are quite full this evening.\n")

money = int(input("How many dollars do you slip the Maitre D?"))

if money:  #数字当作条件进行计算,0是False,其他是True
    print("Ah,I am reminded of a table,Right this way.")
else:
    print("Please,sit.It may be a while.")
    
Welcome to the Chateau D' Food
It seems we are quite full this evening.

How many dollars do you slip the Maitre D?0
Please,sit.It may be a while.
#break和continue语句
count = 0
while True:
    count += 1
    if count > 10:
        break         #break跳出循环
    if count == 5:
        continue      #continue跳回到循环的开头,不会执行print
    print(count)
1
2
3
4
6
7
8
9
10
# 复合条件
print("\t Exclusive Computer Networ")
print("\t\tMembers only!\n")

security = 0

username = ""
while not username:
    username = input("Username:")
    
password = ""
while not password:
    password = input("Password:")
    
if username == "Lzq" and password == "1996":
    print("Hi,",username)
    security = 5
elif username == "Lfp" and password == "1995":
    print("Hi,",username)
    security = 3
elif username == "Lkp" and password == "1994":
    print("Hi,",username)
    security = 2
elif username == "Mjy" and password == "1993":
    print("Hi,",username)
    security = 1
else:
    print("Login failed.You are not so exclusive.\n")
    
	 Exclusive Computer Networ
		Members only!

Username:Lzq
Password:1996
Hi, Lzq
import random

print("This is a little game.")

number = random.randint(1,10)
guss = int(input("Please guss the number."))
tries = 1

while guss != number:
    print("You are wrong.")
    if guss > number:
        print("Higher...")
    else:
        print("Lower...")
    guss =int(input("Please try again."))
    tries +=1

print("Lucily,the number is %d ,and you tried %d"%(number,tries))       
This is a little game.
Please guss the number.5
You are wrong.
Lower...
Please try again.7
You are wrong.
Lower...
Please try again.9
Lucily,the number is 9 ,and you tried 3

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值