Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Ints to Str Problem
#1
Bug 
Hey all,

I am currently trying to simulate a project I am undertaking in Python 3.7, idea is to randomize some serial numbers and write them to a .TXT file and read from it. It creates the .TXT file and reads ok but the serial number never changes (Random) just wondering if anyone could give me some pointers on how to solve this?

My code is below...

import random
from datetime import datetime

# Using random to simulate barcode scan
serial_No = random.randint(100, 10000000000000)
serial_Desc = ["Nimbus Front Case", "Nimbus Pressure Control Assy", "Nimbus Power Cable"]
user_name = ["Grant Peach", "Brett Stewert", "Barry Harnett", "Roberto Delguadio"]
date_now = datetime.now()
cage_Ref = "Repair Cage 1"

while serial_No >= 10000000000:
    print("\nCreating Serial Log...\n")
    text_file = open("Scanner_Log.txt", "a")
    text_file.write("Barcode No: ")
    text_file.write(str(serial_No))
    text_file.write("\n")
    text_file.write("Item Description: ")
    text_file.write(random.choice(serial_Desc))
    text_file.write("\n")
    text_file.write("Barcode Scanned by: ")
    text_file.write(random.choice(user_name))
    text_file.write("\n")
    text_file.write("On: ")
    text_file.write(str(datetime.now()))
    text_file.write("\n")
    text_file.write("From: ")
    text_file.write(cage_Ref)
    text_file.write("\n")
    text_file.write("\n")
    text_file.close()
    print("Serial Log Created\n")
    # Reading TXT File
    print("Now reading the Serial log...\n")
    text_file = open("Scanner_log.txt", "r")
    print(text_file.read())
else:
    print("Error Reading or Creating TXT file")
Thanks in advance Smile
Reply
#2
Hello,
The "random" part of your script (serial_No = random.randint...) is not in the while loop, so it executes only once, and you get only one number. You should put it inside the while loop.
For working with files, you want to use context manager (with) instead of open and close.
Also, having a serial number size as terminate condition of a while loop is a bit odd. To me it sounds more sensible to have a for loop executing a fixed amount of times -> how many serial numbers you want. But that of course is your design decision :)
Reply
#3
That has worked a treat, thanks for your help much appreciated! Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Convert String of an int array to a Numpy array of ints mdsousa 5 5,576 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 6,808 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  Redefine __add__ for ints ihf 4 3,284 Jun-01-2019, 06:29 PM
Last Post: ihf
  Reading floats and ints from csv-like file Krookroo 15 19,900 Sep-05-2017, 03:58 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020