(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