习题3.md

# 随机数的生成
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

猜你喜欢

转载自blog.csdn.net/DMU_lzq1996/article/details/82942835