python 是否存在某种函数,使得变量中的一个选择不会被另一个选择所取代?

nkoocmlb  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(88)

对于一个叫做M的常量,我创建了一个叫做constants的模块,并将M的值写入其中:

from random import choice
l1 = ['a',
      'ability',
      'able',
      'about',
      'above',
      'accept',
      'according'
      'account'
      'across',
      'act',
      'action',
      'activity',
      'actually',
      'add',
      'address',
      'administration',
      'admit',
      'adult',
      'affect',
      'The quick brown fox jumps over the lazy dog',
      'yes,'
      'Good Morning',
      'Good night',
      'Super Ball'
      ]
M = choice(l1)

我试着把它导入,但当我试着:

import datetime
import re

from constants import M

print('''
Welcome to Typing Practice!
We will give some random text.
You should type it again.
The time took will be printed.
Your time starts... NOW!!!
''')

print(f'You must enter {M}')
current_time = datetime.datetime.now()
h = input("")

n = re.compile(r'M')
errors = 0
for letter in M:
    if not n.match(h):
        errors += 1

time_now = datetime.datetime.now()
time_took = time_now - current_time
print("you took: ", time_took, "\n\\\\\\", "Errors:", errors)

我只是得到了你必须进入幕我进入幕,它说:\错误:3这可能意味着M正在更改!!

-----------------------------------
rjee0c15

rjee0c151#

re.compile(r'M')实际上是创建一个匹配字符串“M”的正则表达式,例如,您必须在键盘上键入M才能匹配它。
不要使用正则表达式,只需迭代输入字符串和字符串变量M,然后在一个简单的for循环中比较字符。

相关问题