私はループを使って複数のデータ型(文字と整数の両方)を持つリストを作成しようとしています(forループとwhileループの両方)。pythonでループ(forループとwhileループの両方)を使って複数のデータ型(文字と整数の両方)を持つリストを作成する方法。
次のようにループしている間使用してコードスニペット: -
my_list=[]
item=0
while item!=5:
item=int(input("""Enter the number to create the list(To discontinue enter 5): """))
my_list.append(item)
print("The list created is: ",my_list)
length=len(my_list)
コードは、forループを使用してスニペット: -
list=[]
#Creating the list
count=int(input("Set the length of list as 7, Enter 7: "))
for i in range(count):
item=int(input("Enter the element: "))
list.append(item)
if (count-i==2):
last_item=int(input("Enter the 7th element:"))
list.append(last_item)
break
print("\nThe list created is: ",list)
私のコードは、どちらかだけで、整数や文字列でリストを作成することができます。データ型(つまり、同じリストに整数と文字列の両方)を混在させることはできません。 私のコードで行う必要がある変更を提案してください。
また、私はwhileループを使って実行を中断することができます。リストに複数のデータ型要素がある場合に実行できます。
'[1、 '2'、 '3'、4]'完了。 –
間違っていますか? –
しかしここで2と3は文字列として扱われますか?もし私が間違っていれば私を修正してください –