Home > Backend Development > Python Tutorial > What are the simple python code types?

What are the simple python code types?

coldplay.xixi
Release: 2020-06-20 14:39:58
Original
5075 people have browsed it

What are the simple python code types?

What are the simple python code types?

Simple python code types are:

1. [Background]

Recently, Patrick wanted to lose weight, and he decided to control In your own diet, eat less Krabby Patties.

In order to help his good friend Patrick, SpongeBob and Patrick created an eating game together.

The rules of the game are as follows:

If Patrick eats more than 40 Krabby Patties a day, SpongeBob will be given 100 yuan;

If Patrick eats less than or equal to For 40 Krabby Patties, SpongeBob gave Patrick Star 100 yuan.

[Title]

Please write a piece of code:

When you enter the number of Krabby Patties that Patrick eats in a day, you can print out the number of Krabby Patties that Patrick eats that day. Give SpongeBob money, or SpongeBob give Patrick money.

Tips:

1. Use the input() function to get the number of Krabby Patties eaten by Patrick.

2. Use the int() function to convert a string into a number.

3. Then compare this number with 40.

4. Use if...else... statement.

p =int(input("输入派大星今天吃的汉堡个数"))
if p<40 :
    print("海绵宝宝给派大星100块")
else :
    print("派大星给海绵宝宝100块")
Copy after login

2. Many people don’t like the number 4 because they think it’s a bit unlucky.

Please use while... to let the computer print out the numbers 1, 2, 3, 5, 6, and 7 in sequence, but when it comes to 4, avoid not displaying it.

Tips:

1. Set a variable n to 0, and then start the loop with a certain condition.

2. Let n 1 every time through the loop.

3. To avoid the number 4, you can use conditional judgment if and then print the number.

a=0
while a<7 :
    a += 1
    if a == 4:
        continue
    else :print(a)
Copy after login

3. Write such a code, the rules are as follows:

Ask the user to guess the number of your bank card balance (we set this number to 5).

If the guess is too high, tell the user that it is too much; if the guess is too small, tell the user that it is too small; if the guess is right, tell the user that it is correct.

The user has a total of 3 chances to guess. The program ends after three chances are used up.

b = 5
while a :
    a -= 1
    c = int(input("请输入你猜的银行卡余额(1~10)"))
    if b < c :
        print("你猜大了")
    elif b > c :
        print("你猜小了")
    else :
        print("恭喜你,答对了")
        break
Copy after login

Add some things, such as changing the bank card amount to random. Every time you fail to guess, you will be told how many more chances you have. If you fail to guess correctly after the chance is exhausted, you will be called stupid.

import random
a = 3
b = random.randint(1,10)
while a :
    a -= 1
    c = int(input("请输入你猜的银行卡余额(1~10)"))
    if b < c :
        print("你猜大了,你还有"+str(a),"次机会")
    elif b > c :
        print("你猜小了,你还有"+str(a),"次机会")
    else :
        print("恭喜你,答对了")
        break
else:
    print("你真蠢,这都猜不到,正确答案是"+str(b))
Copy after login

4. Here is a list of movies and actors (a list is nested in the dictionary.)

#原始数据如下:
v = {
&#39;妖猫传&#39;:[&#39;黄轩&#39;,&#39;染谷将太&#39;],
&#39;无问西东&#39;:[&#39;章子怡&#39;,&#39;王力宏&#39;,&#39;祖峰&#39;],
&#39;超时空同居&#39;:[&#39;雷佳音&#39;,&#39;佟丽娅&#39;],
}
Copy after login

When the user enters the actor's name using the input() function, it can be displayed on the screen Print out that XX starred in the movie XX.

Tips:

1. Use for...in... to traverse the data in the dictionary and take out the key (key), which is the movie name.

2. Then use the key to take out the actor names in the dictionary.

3. Then use if A in B as a condition to determine whether data A is in list B, that is, whether the actor's name is in this list.

#原始数据如下:
v = {
&#39;妖猫传&#39;:[&#39;黄轩&#39;,&#39;染谷将太&#39;],
&#39;无问西东&#39;:[&#39;章子怡&#39;,&#39;王力宏&#39;,&#39;祖峰&#39;],
&#39;超时空同居&#39;:[&#39;雷佳音&#39;,&#39;佟丽娅&#39;],
}
a = input("请输入演员的名字:")
for key in v :
    if a in v[key] :
        print(a+"出演了"+key)
Copy after login

Recommended tutorial: "python tutorial"

The above is the detailed content of What are the simple python code types?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template