2017-06-20 6 views
-1

私はまだPythonには初めてです。私はループを使用しようとすると問題が発生します。私は2つの変数を持ち、2つ目の変数は常に私に同じ結果を与えます。私の例では、私はこのようなデータベースいますPython繰り返しの結果は常にdoubleです

id | name 
1 porce 
2 cadilcac 
3 honda 
4 toyota 

はまず、私はそれがうまく働いて、idをループしようとしました。私が欲しいもの

1.porce 
2.porce 
3.porce 
4.porce 

結果は次のとおりです:私はnameをループしようとしたとき、しかし、結果はこのように、常に間違っている

1.porce 
2.cadilcac 
3.honda 
4.toyota 

そして、これは私のコードです:

number = 0 
for id_id in id_number: 
    dd = name # here i call the name by id 
    number += 1 
    print(number) 
    print(dd) 
+0

全体コードを表示します。あなたはおそらく正しい変数にアクセスすることを忘れています – CIsForCookies

+4

あなたはポルシェを意味しますか?また、 'name'とは何ですか? –

+2

申し訳ありませんが、あなたのコードは役に立ちません。ソースのない2つの変数があり、コメントが意味するものは汚れています。 –

答えて

0
# using list 
    vehicles = ["Honda", "Porche", "Cadilcac", "Toyota"] 
    number = 0 
    for vehicle in vehicles: 
     number += 1 
     print(number), # ',' is used to print both columns in a single line 
     print(vehicle) 

    # using dictionary 

    vehicles = {1: 'porce',2:'cardilac',3:'honda',4:'toyota'} 
    number=0 
    for id_id in dict: 
      dd= dict[id_id] 
      number += 1 
      print(number), 
      print(dd) 
+0

それは私のために働いています、ありがとうございます。 –

0

なぜあなたは弟をループするために直接車両リストを使用しませんでしたか?たとえば、

vehicles = ["Honda", "Porche", "Cadilcac", "Toyota"] 
number = 0 
for vehicle in vehicles: 
    number += 1 
    print(number) 
    print(vehicle) 

私はそれが役に立てば幸い:)

0
#set counter 
count = 1 

#set list 
my_list = ['porce', 'cadilac', 'honda', 'toyota'] 

#loop through the values and use the counter to assign to the first part of 
# your print statement 
for item in my_list: 

    # this format would be the same as the one that you requested 
    print('{}.{}'.format(count,item)) 
    # increment the counter 
    count += 1 

However, if you directly import from a database, a set of tuples would be a better structure than that of a list. Otherwise, it would be better to employ a dictionary type structure. 
0
vehicles = ["Honda", "Porche", "Cadilac", "Toyota"] 
number = 0 
for vehicle in vehicles: 
    number += 1 
    print(str(number) + ". " + vehicle) 

私は、これはあなたが何を意味するかであると思います。

関連する問題