LittleDTLe
LittleDTLe

Reputation: 57

PYTHON: IF statement with Parameters from a file not working correctly

I am creating a program that the user can set the odds of a good roll. The variable is stored in a file so that it can be used even when you close the program. My problem is that only the first state of the if statement gets executed and I cannot figure out why.

file = open("rolltype.txt", "r")
user = input("Enter what you want to roll[e.x: 2d8]: ")
if file.readline() == "1":
    print("Favor")
    favor(user)
elif file.readline() == "2":
    print("AGAINST")
    against(user)
elif file.readline() == "3" or file.readline() == None:
    print("NEUTRAL")
    check(user)
file.close()

I open the file in read mode and in the if statement I check 4 possible scenarios, and call the appropriate function. If the file contains the value 1 then the program executes correctly, and if not, the elifs get jumped over.

Side-note: the print() inside the if are used for debugging. Thank you for taking the time to read and maybe try to help. Have a great rest of the day!

Upvotes: 0

Views: 793

Answers (5)

kindall
kindall

Reputation: 184395

There are two problems with your code.

  • readline() on a file includes the newline at the end of the line. It will never match "1" or any other string that doesn't end with a newline.
  • You're comparing a different line each time because of the multiple calls to readline(). Every time readline() is executed, the pointer is moved to the next line. If the file doesn't contain enough lines, some of them are going to return the empty string, and it won't match anything either.

Solution: call readline() once into a variable and use strip() to take off the newline (and any other whitespace).

num_from_file = readline().strip()
if num_from_file == "1":

Upvotes: 4

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

You can simplify your code by using dictionaries to define a value to print mapping print_dct , and value to function mapping func_dct Also you can use with to open your file, so that you would not need to close it explicitly.


user = input("Enter what you want to roll[e.x: 2d8]: ")

print_dct = {"1": "Favor", "2": "AGAINST", "3": "NEUTRAL"}
func_dct = {"1": favor, "2": against, "3": user}


with open("rolltype.txt", "r") as file:
    value = file.readline()

    if value == None:
        print(print_dct['3'])
        check(user)

    elif value in print_dct:
        print(print_dct[value])
        func_dct[value](user)

Upvotes: 1

Max Teiger
Max Teiger

Reputation: 145

I am not sure I understand your problem, but I already encounter something similar. It was because the use of readline() multiple times. Can't you just use :

fileContent = file.readline()
user = input("Enter what you want to roll[e.x: 2d8]: ")
        if fileContent == "1":
            print("Favor")
            favor(user)
        elif fileContent == "2":
            print("AGAINST")
            against(user)
        elif fileContent == "3" or fileContent == None:
            print("NEUTRAL")
            check(user)
        file.close()

I am not sure about my answer because I don't understand how your file is written.

Upvotes: 1

Preston Hager
Preston Hager

Reputation: 1631

Every time you call file.readline() it reads a new line from the file. This can be fixed by assigning it to a variable before the if statements.

line = file.readline()
if line == "1":
  print("Favor")
elif line == "2":
  print("Against")
elif line == "3":
  print("Nuetral")

However, I don't think that readline always trims trailing whitespace. Therefore, I suggest using file.readline().strip().

Upvotes: 2

Tim Klein
Tim Klein

Reputation: 2798

Every time you call file.readline(), it is reading the next line in the file.

Therefore, you will get a different result for every conditional expression.

In order to solve this, you should simply store the value in a variable and check that in your conditional expressions:

user = input("Enter what you want to roll[e.x: 2d8]: ")
value = file.readline()

if value == "1":
    print("Favor")
    favor(user)
elif value == "2":
    print("AGAINST")
    against(user)
elif value == "3" or value == None:
    print("NEUTRAL")
    check(user)
file.close()

Upvotes: 3

Related Questions