Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Recommended Posts

Posted · Original PosterOP

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!

Link to post
Share on other sites
9 minutes ago, SamAnw said:

and it won't read after the first line of the file

You're only printing once, so obviously you won't be getting more than one line of output...


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
Posted · Original PosterOP
8 minutes ago, WereCatf said:

You're only printing once, so obviously you won't be getting more than one line of output...

I have no idea how to print it more than once...I've just started learning python so I'm missing some base ya know :)

It would be great if you could help!

 

Thankyou!!

Link to post
Share on other sites
22 minutes ago, SamAnw said:

I have no idea how to print it more than once...I've just started learning python so I'm missing some base ya know :)

It would be great if you could help!

I'll give you a few tips, instead of any full solution:

#This is really clunky    
	if state[i].startswith(neg):
    
#Replace with simply
	if state[i] == "BROKEN":

Or you could replace the whole while-loop with something like e.g.:

for itemState in state:
	if itemState == "BROKEN":
		n += 8/(2**i)
	else:
		print("invalid entry 1")
	i+=1

As for the print-clause, you need to either put your print-statement in the above loop or make a separate loop for it, if you want more than one line of output.


Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
21 hours ago, WereCatf said:

As for the print-clause, you need to either put your print-statement in the above loop or make a separate loop for it, if you want more than one line of output.

This is the main issue. Your print statement only executes once. However you want the print statement to run for each line in the file, so a loop is a good choice.

 

At the start of your program "a = file.read()" reads the entire file into the variable "a". One option is to use a for loop over the lines in the file:

 

txt = "Prob06.in.txt"
file = open(txt,"r")
for line in file:
    state = line.split()
    pos = "W"
    neg = "B"
...
    LED1= int((n - LED2)/4)
    print(LED[LED1], LED[LED2])

Notice that from the line "state = line.split()" to print are all indented, putting them inside that for loop.

 

 

Here is my complete rewrite of your program for your perusal (and because I want to look/feel clever). It is obviously different from your program, so don't turn it in as your own homework:

STATES = {"BADGER": 0, "MUSHROOM": 1}
LED = ("off", "red", "green", "blue")
with open("in") as f:
    for ln, line in enumerate(f):
        try:
            bits = [STATES[i] for i in line.split()]
            led1, led2 = 2*bits[0] + bits[1], 2*bits[2] + bits[3]
            print(LED[led1], LED[led2])
        except:
            print(f"Bad line: {ln}")

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×