Learning to program - The Basics - Talking to the user
# create an empty address book dictionary
addressBook = {}# read entries till an empty string
name = raw_input("Type the Name - leave blank to finish")
while name != "":
entry = raw_input("Type the Street, Town, Phone. Leave blank to finish")
addressBook[name] = entry
name = raw_input("Type the Name - leave blank to finish")# now ask for one to display
name = raw_input("Which name to display?(blank to finish)")
while name != "":
print name, addressBook[name]
name = raw_input("Which name to display?(blank to finish)")
これ最初のprintとか何の為にあるんだろう。一応#でコメントアウトしてみても意味なしというか違いがわからない。
ちなみにdictionaryの型なので {}で辞書は宣言?する。で最初やっててなんでエラーじゃ!と思ってた。はList型?だから数字しか受け付けてくれない?
後、色々なことを忘れてきている気がするがまた支障が出たら戻っていくことにする。
Also note that whereas in the raw materials example we used a list to store the data as separate fields we have just stored it as a single string here. That's because we haven't yet covered how to break down a string into separate fields. We'll cover that in a later topic too.
あぁ、気になっていたことはここに書いてあった。
VBではPythonで!=と書いてノットイコールとするところをこう書くのか。ちょっと戸惑ったぞおい。Pythonではこう書けないのかな。逆にVBでPythonみたいには書けないんだろうか。
While name <> ""
entry = InputBox("Enter Details - Street, Town, Phone number",
"Address Book Entry")
の部分がどうなるのだろうと思って、VBのスクリプトどういう風に動くのかなぁって思ってコピペしてIEで表示してみたらエラーの表示、左下のエラーボタンクリックするともろ、そのどうなるのだろうと気になった行。とりあえず二行に渡っているのがいけなかったのか、空白を消して、
entry = InputBox("Enter Details - Street, Town, Phone number", "Address Book Entry")
としたら動くようになった。
ふーむ、"Address Book Entry"というのは入力を促すダイアログが出てくるんだけど、そのダイアログの題名ということだったらしい。なるほどねえ。
後は特に疑問はないので進む、まぁ、dictあたりが不思議な動きしてんなぁという感じではあるが。今は特に詳しく学ぶ必要もないだろう。
A word about stdin and stdout
The main practical use for this is to get around the fact that print always puts a space between the output values, whereas with stdout we can avoid that. Compare the two output lines in the example below:
なるほどねぇ。
そしてこのプログラムでわかった。何故空白の何も出力しないprintがさっきのソースにあったか。空白のprintを抜くと改行されないからか。
print item, # comma suppresses newline
この文でやっと気づいた。
inp = raw_input()
while inp != '':
print inp
inp = raw_input()
これで試すとEOFだとかいうエラーが出るけど。まぁいいや、無視しよう。最終行でなんたらってやつだと思うんだけど。
それにsplitとかいうのもいまいちわかってないが、それは改行関係っぽいからいいや。
Command Line Parameters
ここも若干わかっとらんが、引数のことだろ。argumentって。遂にきたな。という感じだけど、まぁ適当な理解で進んでもなんとかなるだろう。こういう便利なものがあるっつー認識ぐらいで。
よし、この章やっと終わり。色々なことしてたから長かった。