简体   繁体   English

在python错误上测试2个字符串的相等性

[英]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 我想测试条目中的值是否相等,但它什么也没返回,为什么默认参数不能学习值str(line)我也尝试用str的行

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

The return value of sys.stdin.readline() retains the newline, so the value of line is 'random\\n' , not 'random' . sys.stdin.readline()的返回值保留换行符,因此line的值是'random\\n' ,而不是'random' You need to strip it first: 您需要先剥离它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM