Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outputs missing
#1
Hello,

I've been working on a python program that reads a file instead of inputs. However, there seems to be a problem in my program and it won't read after the first line of the file, so I'm only getting part of the outputs.

Here's the file:

Prob.06.in

WORKING WORKING WORKING WORKING
WORKING BROKEN BROKEN WORKING
BROKEN BROKEN BROKEN BROKEN



And here's the code:
from math import *
txt = "Prob06.in.txt"
file = open(txt,"r")
a=file.read()
state = a.split()
pos = "W"
neg = "B"
i=0
n=0
while i < 4:
    if state[i].startswith(neg):
        n += 8/(2**i)
    elif not state[i].startswith(pos):
         print("invalid entry 1")
    i+=1
LED = ('off','red','green','blue')
LED2= int(n % 4)
LED1= int((n - LED2)/4)
print(LED[LED1], LED[LED2])
The output of this program should be:

off off

red green

blue blue



But the output I'm getting is:

off off



Thankyou in advance!
Reply
#2
(Feb-12-2020, 04:50 AM)SamAnw Wrote: print(LED[LED1], LED[LED2])

Your only printing two things. Maybe you mean to indent things so that it's all in your loop?
Reply
#3
a=file.read()
This reads the entire file into A
so after line 4, add
for line in a:
    state = line.strip().split()
    ...
remove current line 5
also use more meaningful names, single letter names will get you in trouble quickly because you won't be able to remember what's what.
Reply
#4
(Feb-12-2020, 06:21 AM)michael1789 Wrote:
(Feb-12-2020, 04:50 AM)SamAnw Wrote: print(LED[LED1], LED[LED2])

Your only printing two things. Maybe you mean to indent things so that it's all in your loop?

Yes basically that's it, but I dont know how to do that :/

(Feb-12-2020, 09:29 AM)Larz60+ Wrote: a=file.read()
This reads the entire file into A
so after line 4, add
for line in a:
state = line.strip().split()
...

remove current line 5
also use more meaningful names, single letter names will get you in trouble quickly because you won't be able to remember what's what.

Hey!
So I changed my code according to your suggestions and this is what the code looks like:
from math import *
txt = "Prob06.in.txt"
file = open(txt,"r")
a=file.read()
for line in a:
    state=line.strip().split()
pos = "W"
neg = "B"
i=0
n=0
while i < 4:
    if state[i].startswith(neg):
        n += 8/(2**i)
    elif not state[i].startswith(pos):
         print("invalid entry 1")
    i+=1
LED = ('off','red','green','blue')
LED2= int(n % 4)
LED1= int((n - LED2)/4)
print(LED[LED1], LED[LED2])
And now this is the output I'm getting:
invalid entry 1
Traceback (most recent call last):
File "C:\Users\admin\Desktop\Prelim_Prog\Problem_06\Prob06.in.py", line 12, in <module>
if state[i].startswith(neg):
IndexError: list index out of range
Reply
#5
you only have item in state. In addition I think the word 'file' is reserved (I'm not sure though).
You should have something like this:

file = open(txt,"r")
a=file.read()
state = []
for line in a:
    state.append([line.strip().split()])
or if that doesn't work:
   state.append(line.strip().split())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,691 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 592 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,126 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,340 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,447 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,648 Mar-15-2021, 02:43 PM
Last Post: rybina
  ONE input => THREE outputs Tricia279 6 2,559 Jan-14-2021, 08:52 AM
Last Post: perfringo
  Multi set string inputs/outputs kwmcgreal 2 2,007 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,161 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Interpreter and running a .py file give different outputs PythonNPC 5 2,945 Jul-21-2019, 01:07 PM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

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