2017-06-06 11 views
0

私は以下のコードを持っています。ユーザーが選択したときに私はそれが第二の要素を取る3、ユーザーが選択したとき、それは第一要素 を取る2、ユーザーが選択したとき、それは0番目の要素 をとる1、すなわち0とインデックス作成を開始したリストの依存関係 でPython Text Game:プログラムの改善

を打っています..私はこれのように行く。

私はこれを克服したいと思っていますが、内部的に与えられたtext = text-1を変換するようなものはありません。助けてください。コメントで解決他のソリューションと

import numpy as np 
Ran=[np.random.randint(1,5)] 
Val=Ran[0] 
print(Val) 
#Items 
items=['1. pot plant','2. painting','3. vase','4. lampshade','5. shoe'] 
print ("\n") 
#Intro Text 
print ("Last night you went to sleep in the comfort of your own home.") 
print ("Now, you find yourself locked in a room. You don't know how") 
print ("you got there or what time it is. In the room you can see") 
print ("\n") 
print (len(items), "Things:") 
for x in items: 
    print (x) 
print ("") 
print ("The door is locked. Could there be a key somewhere?") 
print ("Enter the corresponding number of thing which you \ 
would like to check..Yougot only 3 chances !! ") 
k=0 
while (k==0): 
    Ins1 = int(input()) 
    if (Ins1 == Val): 
     print("You're Lucky! Got the key in First instance ") 
     break 
    else: 
     c=items[Ins1] 
     print("Damn ! key is not available in ", c , "Try again..") 
    Ins2 = int(input()) 
    if (Ins2 == Val): 
     print("Got the key on your 2nd attempt") 
     break 
    else: 
     c=items[Ins2] 
     print("Bad luck ! Try again..not in ", c ,"your last attempt") 
    Ins3 = int(input()) 
    if (Ins3 == Val): 
     print("Finally you got the key") 
     break 
    else: 
     c=items[Ins3] 
     print("you're done. Die here :(Key not in ",c,) 
     break 
+0

だけitems' 'の先頭にダミーの '' 値を置きます。 – PaulMcG

+0

しかし、ユーザはオプションを印刷するときにダミー値を表示します。 –

+0

ちょうど1から始まる: 'for item in item [1:]:print(x)' – PaulMcG

答えて

0

Just put a dummy '' value at the beginning of items. – Paul McGuire 

But the user will view the dummy value, as am printing the options to him. – Abdul Salam 

Just start at 1 then: for x in items[1:]: print(x) – Paul McGuire 

Thanks Paul ! It works Fine :)