【问题标题】:How to generate different license plates and save them to a file?如何生成不同的车牌并将它们保存到文件中?
【发布时间】:2023-03-03 22:14:01
【问题描述】:

我正在制作一个生成西班牙车牌的 python 程序,我想让程序询问你想要多少车牌(保存在“数量”变量中),然后生成那么多。

我也想知道是否可以将输出保存到文件中。

提前致谢!

import random
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
nums = '0123456789'
letters = ''
numbers = ''
quantity = int(input('Cuántas matrículas quieres? '))
for c in range(3):
    letters += random.choice(chars)
    
for c in range(4):
    numbers += random.choice(nums)

print('Tu matrícula es', numbers, letters)

【问题讨论】:

  • 像这样写入文件:file = open('file_name.txt', 'w') file.write('your_text') file.close()

标签: python loops file


【解决方案1】:

您可以使用列表推导:

for i in range(quantity):
    print('Tu matrícula es', ''.join([random.choice(chars) for i in range(3)]+[random.choice(nums) for i in range(4)]))

数量 = 3 的示例输出:

Tu matrícula es OBR8830
Tu matrícula es VXW4958
Tu matrícula es PWU6749

更新:如果您需要字母和数字之间的空格:

for i in range(quantity):
  print('Tu matrícula es', ''.join([random.choice(chars) for i in range(3)]+[' ']+[random.choice(nums) for i in range(4)]))

样本输出:

Tu matrícula es GHT 3077
Tu matrícula es EQJ 3065
Tu matrícula es FQI 1923

【讨论】:

    【解决方案2】:

    在python中你可以这样做:

    >>> 5 * "tT"
    'tTtTtTtTtT'
    

    所以对于你的例子:

    output = quantity * (numbers + letters)
    print(output)
    

    对于写入文件:

    with open('result.txt', 'w') as file:
        file.write(str(output))
    

    【讨论】:

      【解决方案3】:

      在这里我们生成所需数量的不同西班牙车牌,并将它们保存到一个文件中:

      import random
      
      def generate_spanish_plate():
          chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          nums = '0123456789'
          letters = ''
          numbers = ''
          for c in range(3):
              letters += random.choice(chars)
          for c in range(4):
              numbers += random.choice(nums)
          return f'{numbers} {letters}'
      
      quantity = int(input('Cuántas matrículas quieres? '))
      
      plates = []
      
      for i in range(quantity):
          new_plate = generate_spanish_plate()
          # Verify plate is hasn't been generated
          while new_plate in plates:
              new_plate = generate_spanish_plate()
          plates.append(new_plate)
      
      # Save to file
      with open('spanish_plates.txt', 'w') as file:
          file.write(str(plates))
      
      
      

      【讨论】:

      • @MadeBy Alvaropop,这会生成不同的车牌号并将它们保存到文件中 - 尽情享受吧!
      【解决方案4】:

      您可以在函数中生成车牌,然后循环调用它:

      import random
      chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
      nums = '0123456789'
      quantity = int(input('Cuántas matrículas quieres? '))
      
      def create_plate():
          letters = ''
          numbers = ''
          for c in range(3):
              letters += random.choice(chars)
          
          for c in range(4):
              numbers += random.choice(nums)
      
          return numbers + ' ' + letters
      
      for i in range(0, quantity):
          print('Tu matrícula es', create_plate())
      

      【讨论】:

      • 谢谢!代码有效,但您忘记在 for i in range 中添加“in”
      • 如何在数字和字母之间添加空格?
      • @MadeByAlvaropop 关于in 的好消息——确实,错过了。编辑和修复。关于空间 - 只需在 return 语句中构建字符串时添加它。请参阅我的更新答案。
      • 我已经修复了空间,只需要在中间添加一个空字符串。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多