A simple Python program structure exercises

1. guessing game.

Preset in the program integer between 0 to 9, so that the user through the keyboard guessing, if greater than the preset number,

Display "Unfortunately, too big"; if less than the predetermined number, displays "Unfortunately, too small"; and so on, until the number guessed, shows "pre

Measured N times, you guessed it! ", Where N is the number of times a user input numbers.

from random import*
x=randint(0,9)

for i in range(10):
    y=eval(input("请输入一个0到九的数:"))

    if x<y:
        print("遗憾,太大了")
    elif x>y:
        print("遗憾,太小了")
    else:
        print("预测"+str(i+1)+"次,""你猜中了")
        break

Here Insert Picture Description
This problem is called randint random library () function to generate a random integer, a user input digital inside the for loop compares the random number,

Tip By adjusting the size of the digital input, when the input is equal to the generated random number with the numbers, break out of the loop, the program ends.

2. Statistics different number of characters.

User keyboard input from his string, write a program in which the output statistics and English, numbers, spaces and other

The number of characters.

x=input("输入你的字符串:")
a=b=c=d=0
for i in x:
    if ord('a')<=ord(i)<=ord('z') or ord('A')<=ord(i)<=ord('Z'):     
        a=a+1
    elif ord('0')<=ord(i)<=ord('9'):
        b=b+1
    elif ord(i)==ord(' '):
        c=c+1
    else:
        d=d+1
print("这一行字符中字母的数量是:{},数字的数量是:{},空格数量是:{},其他字符的数量是:{}。".format(a,b,c,d))

Here Insert Picture Description

Built-in function order () Returns the value of a single character encoded Unicode representation. Forming four kinds of character initial value 0, for looping through the input character string,

The number of characters to distinguish character classes by determining the sentence and traverse increments.

3. guessing game. Reprogramming exercises 4.1, let the computer randomly generates a preset number ranging between 0 and 100, other rules of the game change.

from random import*
x=randint(0,100)

for i in range(100):
    
    try:
        y=eval(input("请输入一个0到100的数:"))
        if x<y:
            print("遗憾,太大了")
        elif x>y:
            print("遗憾,太小了")
        else:
            print("预测"+str(i+1)+"次,""你猜中了")
            break
    except NameError:
        print("输入格式有误!")
    finally:
        print("执行完毕!")

Here Insert Picture Description

Simple addition of exception handling, processing non-numeric type

4.1 with a modified 5 exception handling, so that it can receive and process any input from the user.

from random import*
x=randint(0,9)
print("{0:*^50}".format("猜数字游戏"))
for i in range(10):
    
    try:
        y=eval(input("请输入一个0到9的数:"))
        if x<y:
            print("遗憾,太大了")
        elif x>y:
            print("遗憾,太小了")
        else:
            print("预测"+str(i+1)+"次,""恭喜你,你猜中了!")
            break    
    except NameError:
        print("输入格式有误!")
    else:
            print("没关系,继续加油!")   
    finally:
        print("这是第{}猜".format(i+1))
print("{0:*^30}".format("游戏结束"))

Here Insert Picture Description

This problem is relatively optimized version of a title and title IV, adding additional content else, as well as each cycle is finished finally statement prompt to the user.

Published 705 original articles · won praise 833 · Views 1.36 million +

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/105202260
Recommended