Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:07:51+00:00 2026-05-27T05:07:51+00:00

in Python I’m trying to set a variable from info stored in a txt.

  • 0

in Python I’m trying to set a variable from info stored in a txt. I can manage to set it, but it does not act as a number. Is it being stored as something else?
Here is my .txt file (all info is written on one line, no \ns):

11 14 15 3

Here is my script:

def break_words(text):
    words = text.split(' ')
    return words

file_name = open("set_initial.txt")
words = break_words(file_name.read())

shift_l1 = words[0]
shift_l2 = words[1]
shift_l3 = words[2]
shift_l4 = words[3]
shift_l5 = words[4]
shift_l6 = words[5]

# this part is to verify that the variables are being set:
print shift_l1, shift_l2, shift_l3, shift_l4

while shift_l4 < 28:
# and the script goes on into a loop from here

I am using this method because the length of the values in the txt will change (for example to: 114 34 2 4318). When I run the script, the print function works fine and returns my variables as the numbers I have in my .txt (11 14 15 3 respectively), so shift_l4 prints out as 3, so my WHILE loop should be functioning. But it’s not. As I said, I figure my variables aren’t being set to the number value of the numbers in the .txt, but maybe just the text value? I don’t know how to fix it though. Any help or ideas?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:07:52+00:00Added an answer on May 27, 2026 at 5:07 am

    “file_name” is not a good name for a file object. “words” is not a good name for a collection of numbers represented as strings. “shift_l5 = words[4]” and “shift_l6 = words[5]” will fail, because you have only 4 numbers.

    Note that print "3" and print 3 produce the same results. Use print repr(something) instead of just print something to get a handle on what data you actually have.

    Try this:

    f = open("set_initial.txt")
    numbers = [int(n) for n in f.read().split()]
    print numbers
    assert len(numbers) == 4
    shift_l1, shift_l2, shift_l3, shift_l4 = numbers
    print shift_l1, shift_l2, shift_l3, shift_l4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python's access to environment variables does not accurately reflect the operating system's view of
Python does not provide built-in support for multi-dimensional arrays. I need to develop an
Python's arbitrary precision decimals are lovely, but I can't seem to find a way
Python is pretty clean, and I can code neat apps quickly. But I notice
Python's os.path.getctime on the Mac (and under Unix in general) does not give the
Python beginner here. I've looked around and found similar questions but can't quite cobble
Python works on multiple platforms and can be used for desktop and web applications,
Python's convention is that variables are created by first assignment, and trying to read
Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called
Python decorators are fun to use, but I appear to have hit a wall

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.