Amal Kostali Targhi
Amal Kostali Targhi

Reputation: 903

test equality of 2 string on python error

from optparse import OptionParser
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
import sys
print("Please choose the type of agent")
line = sys.stdin.readline()

i have put random and when i test what line looks like it give me random

parser.add_option("-p","--player1",dest="player1",
                  default=str(line),help="Choose type of first player")

i want to test if the value in entry are equal but it returns nothing why the default parameter can't learn the value str(line) i try also for line withour str

if str(opts.player1)=='random':
    print ('true')

Upvotes: 0

Views: 35

Answers (1)

chepner
chepner

Reputation: 532538

The return value of sys.stdin.readline() retains the newline, so the value of line is 'random\n', not 'random'. You need to strip it first:

parser.add_option(..., default=str(line.strip()), ...)

Upvotes: 1

Related Questions