Jump to content

SamAnw

Member
  • Posts

    11
  • Joined

  • Last visited

Reputation Activity

  1. Agree
    SamAnw reacted to minibois in Python time   
    split the strings at the s an m marks (and h for hour) and place it into a a new string with colons inbetween.
    do you not have required reading material for this sort of homework that can be consulted? Because otherwise, Googling stuff effectively is also a trait a programmer should build up.
  2. Agree
    SamAnw reacted to Mijnay in Outputs missing   
    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}")  
  3. Like
    SamAnw reacted to WereCatf in Outputs missing   
    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.
  4. Informative
    SamAnw reacted to WereCatf in Outputs missing   
    You're only printing once, so obviously you won't be getting more than one line of output...
×