The first stage: built-in method development infrastructure day07 Python variable homework

1. business logic code realizing the following

Write code, the following variables name = " aleX", follow required to achieve each function:

  1. Removing spaces on both sides of the value corresponding to the variable name, and outputs the processing result

  2. Determining whether the value corresponding to the variable name beginning with "al", and outputs the result

  3. Determining whether the value of the corresponding variable name ending in "X", and outputs the result

  4. The variable name corresponding to the value of "l" is replaced with "p", and outputs the result

  5. The variable name corresponding to the value in accordance with "l" division, and outputs the result.

  6. The name corresponding to the variable value becomes capital, and outputs the result

  7. The name corresponding to the variable value becomes lower case, and the output

  8. Please output of the two character values ​​corresponding to the variable name?

  9. Please output the first three characters of the name value of the corresponding variable?

  10. Please output the value of two variables corresponding character name?

  11. Please name the output value of the variable corresponding to the index where the "e" position?

  12. Acquisition sequences, removing the last character. Such as: oldboy is acquired

    o
    l
    d
    b
    o

    answer:

# 给的示例
name = " aleX"

# 1.移除 name 变量对应的值两边的空格,并输出处理结果
name1 = name.strip()
print(name1)
# 2.判断 name 变量对应的值是否以 "al" 开头,并输出结果
bool_start = name.startswith('al')
print(bool_start)

# 3.判断 name 变量对应的值是否以 "X" 结尾,并输出结果
bool_end = name.endswith('X')
print(bool_end)

# 4.将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
name_replace = name.replace('l','p')
print(name_replace)

# 5.将 name 变量对应的值根据 “l” 分割,并输出结果。
name_split = name.split('l')
print(name_split)

# 6.将 name 变量对应的值变大写,并输出结果
name_upper = name.upper()
print(name_upper)

# 7.将 name 变量对应的值变小写,并输出结果
name_lower = name.lower()
print(name_lower)

# 8.请输出 name 变量对应的值的第 2 个字符?
print(name[2])

# 9.请输出 name 变量对应的值的前 3 个字符?
print(name[:3])

# 10.请输出 name 变量对应的值的后 2 个字符?
print(name[-2:])

# 11.请输出 name 变量对应的值中 “e” 所在索引位置
print(name.find('e'))

# 12.获取子序列,去掉最后一个字符。如: oldboy 则获取
for i in range(len(name)-1):
    print(name[i])

2. Write guess the age of the game, have the following requirements

(Reference: https://www.cnblogs.com/nickchen121/p/11069989.html )

  1. There may be a user accidentally enter a space after the input of age, such as 18, please do deal
  2. There may be a malicious user can input causes the program error, such as 逗你玩呀, please do deal
  3. If the user does not guess right three times, you can choose to continue playing or quit (custom exit conditions)
  4. If you guess right, you can choose two of the following prizes in the prize (prize can only choose one):{0:'buwawa',1:'bianxingjingang',2:'aoteman',3:'《python从入门到放弃》'}
  5. After the user selects a prize to exit the program, the user can not choose a prize to exit the program.
  6. The idea is not normal, it would be against my first knock three times.

answer:

import random
# 随机一个年龄出来
age = random.randint(0,120)
# 奖品列表
price_dict = {0:'buwawa',1:'bianxingjingang',2:'aoteman',3:'《python从入门到放弃》'}
# 定义一个变量用来存放游戏次数
count = 0
while True :
    guess_age = input('请输入你猜测的年龄(范围在0-120之间):')
    # 先清除用户输入的时候可能存在的空格
    guess_age = guess_age.strip()
    # 判断用户输入的信息是不是都是数字
    if guess_age.isdigit() != True:
        print('你的输入有误,请重新输入')
        continue
    guess_age = int(guess_age)
    if guess_age > age:
        print('对不起,你猜大了,请重新猜一次哈')
    elif guess_age < age:
        print('对不起你猜小了请重新猜')
    else:
        choose = int(input('恭喜你猜对了,可以挑选奖品了,输入0-3进行选择:'))
        print(f'你选择的奖品是:{price_dict[choose]}')
        break
    count += 1
    if count == 3:
        num = input('三次机会用完了,是否继续玩(继续请按y,退出请按q):')
        if num == 'y':
            count = 0
            continue
        elif num == 'q':
            print('你选择了退出游戏')
            break
        else:
            print('你的输入有误,已退出游戏')
            break

blog address

Xiao-long Learning Center

Guess you like

Origin www.cnblogs.com/foreversun92/p/11290579.html