ここにPythonプログラムがあります。ここでは、入力文字列devに文字列PHPをretrunする必要があります。しかし、これは適切に動作していません。文字列を読み込んで別の文字列を返すPythonプログラム
#!/usr/bin/python
str = raw_input("enter the string to be printed:")
print(str)
if str == php :
print("dev")
else :
print("php")
ここにPythonプログラムがあります。ここでは、入力文字列devに文字列PHPをretrunする必要があります。しかし、これは適切に動作していません。文字列を読み込んで別の文字列を返すPythonプログラム
#!/usr/bin/python
str = raw_input("enter the string to be printed:")
print(str)
if str == php :
print("dev")
else :
print("php")
私はあなたに行くための正しい方法をお見せしましょう:
サンプルsimple.py
スクリプト:
s = raw_input("enter the string to be printed:")
words = ('dev','php')
print 'entered: ', s
if s in words:
print 'got: ', words[~words.index(s)]
今
、用法:
A)dev
文字列を入力する場合:
python simple.py
enter the string to be printed:dev
entered: dev
got: php
b)に入るphp
文字列:
python simple.py
enter the string to be printed:php
entered: php
got: dev
をお楽しみください!
私は試したことがあるようにifループでそれを明示することができます – sysadmincrispy
@sysadmincrispy、ポイントは私が最適化されたソリューションを提案しているということです。あなたのコードは 'NameError:name 'php'が定義されていない' throwし、 'dev'、' php'の値だけを考慮する必要があれば柔軟に見えません – RomanPerekhrest
'if str == 'php':'、あなたは引用符がありません。 –