Python clock in the second day

Python clock in the second day

Famous conditions and cyclic structure

Condition: If... Then, I suddenly remembered the sentence making in the elementary school Chinese class

if expression:
    expr_true_suite
else:
    expr_false_suite

The else part can be omitted, or the condition structure can be nested in the else. When you need to check whether multiple expressions are true, you can use elif statement instead of else if

if expression1:
    expr1_true_suite
elif expression2:
    expr2_true_suite
    .
    .
elif expressionN:
    exprN_true_suite
else:
    expr_false_suite

"Assert": When assertthe condition after assert is False, the program automatically crashes and throws AssertionErroran exception.

Loop: execute when...is true...

while 布尔表达式:
    代码块

Of course, while and else can also be used together, when...is true, execute...otherwise...

There is also a traversal loop

for 迭代变量 in 可迭代对象:
    代码块

Often used in conjunction with the range() function, range provides the traversal range

The enumerate() function plays the role of the for+range combination and can also return the serial number

breakThe statement can jump out of the loop of the current layer.

continueTerminate the current cycle and start the next cycle.

passStatement means "do nothing" if you do not write any statement in place that require a statement, then the interpreter displays an error message, but passthe statement is used to solve these problems. (Just like pass, OK, next one when playing games)

List comprehension

Lu Xun once said that science is to say something in words that others can’t understand (Lu Xun: I haven’t said it), and list comprehension translation is to save the results of conditions or loop statements in a list.

Tuple comprehension

With the above example, let’s not talk about it

Set comprehension

To be honest, I don’t even bother to type the words deduction

Well, what needs to be explained is the representation of list [], tuple (), set {}

Exercise:

1. Write a Python program to find numbers that are divisible by 7 and 5, between 1500 and 2700.

# your code here
for x in range(1500,2701):
	if x % 5 == 0 and x % 7 == 0:
	print x
   
   

2. Tortoise and Hare Race

Title description:

It is said that there are all kinds of rabbits and tortoises in the world, but research has found that all rabbits and tortoises have a common characteristic-like racing. As a result, tortoise and hare races are constantly taking place in every corner of the world. Xiaohua is very interested in this, so he decides to study the races of different rabbits and tortoises. He found that although rabbits run faster than tortoises, they have well-known problems-pride and laziness. So in a game with tortoises, once the rabbits find themselves ahead of t meters or more after any second, they will stop. Rest for s seconds. For different rabbits, the values ​​of t and s are different, but all tortoises are the same-they never stop when they reach the end.

However, some competitions are quite long and the whole viewing process will take a lot of time. Xiaohua found that as long as the data of the rabbit and the tortoise are recorded after the start of each game-the rabbit's speed v1 (which means that the rabbit can run v1 meters per second), The speed v2, the t and s values ​​corresponding to the rabbit, and the length of the track l-can predict the outcome of the race. But Xiaohua is very lazy and doesn't want to use manual calculations to guess the result of the game, so he found you-a talented student from the Department of Computer Science of Tsinghua University-for help, please write a program, for the input data v1 of a game, v2, t, s, l, predict the outcome of the game.

enter:

The input is only one line and contains five positive integers v1, v2, t, s, l separated by spaces, where (v1, v2<=100; t<=300; s<=10;l<=10000 and v1 , common multiple of v2)

Output:

The output consists of two lines. The first line outputs the result of the competition-a capital letter "T" or "R" or "D", which means that the turtle wins, the rabbit wins, or both reach the end at the same time.

The second line outputs a positive integer, indicating the time (in seconds) it takes for the winner (or both sides) to reach the end.


Sample input:

10 5 5 2 20

Sample output

D
4

# your code herelist_1=[]
v1,v2,t,s,l=input("请输入用空格隔开的五个正整数,v1,v2,t,s,l其中(v1,v2< =100;t< =300;s< =10;l< =10000且为v1,v2的公倍数)").split()
list=[v1,v2,t,s,l]
for i in list: 
    list_1.append(data)
v_1,v_2,add,ts,lu=list_1   
tt=lu/v_2  
t_2=0
t_1=0
lu_1=0
flag=1
while flag:
    t_1 = t_1+1
    t_2 = t_2 + 1
    lu_1 = t_2 * v_1
    if lu_1 >= lu:
        flag=0
    elif (lu_1-t_2*v_2)>=add:
        t_1=t_1+ts
if t_1 > tt:   
    print("T")
    print(tt)
elif t_1 < tt:
    print("R")
    print(t_1)
else:
    print("D")
    print(t_1)


   

Guess you like

Origin blog.csdn.net/sinat_39470268/article/details/107550008