嘿,所以我尝试使用此功能以及带有用户输入()的while循环来添加到字典中,例如:用户输入=艺术家名称,专辑名称和曲目号,这些名称存储在字典中,一次用户输入完成,以从输入数据中打印新词典。
与if input == 'q' to quit.
谢谢,如果我的问题不清楚,很抱歉,因为这是我关于SOF的第一个问题。
def make_album(artist_name, album_name, track_number):
"""Returns a dictionary of an Artist"""
full_block = {'artist': artist_name, 'album': album_name, 'track': track_number}
return full_block
musician = make_album('Jimi Hendrix', 'The experience', 23)
print(musician)
答案 0 :(得分:1)
在这里!
def make_album(artist_name, album_name, track_number):
"""Returns a dictionary of an Artist"""
artist_name = artist_name.strip() #Removes any spaces in the beginning or end
album_name = album_name.strip() #Removes any spaces in the beginning or end
track_number = track_number.strip() #Removes any spaces in the beginning or end
full_block = {'artist': artist_name, 'album': album_name, 'track': track_number}
return full_block
while True: #I enter into the while loop
block = input("Enter artist, album, track seperated by comma")
#Seperate artist, album, track by a COMMA. This is crucial
if block == 'q':
print("You pressed q, quitting...")
break #I break out of the while loop if block which is the string variable that stores the input == 'q'
else:
album = block.split(",") #Split the string by comma into a list where each element will be the contents of the dictionary.
musician = make_album(album[0], album[1], album[2])
#Element in index 0 is artist_name
#Element in index 1 is album_name
#Element in index 2 is track_number
print(musician)
如果需要,请分别为三个详细信息分别输入:
def make_album(artist_name, album_name, track_number):
"""Returns a dictionary of an Artist"""
artist_name = artist_name.strip()
album_name = album_name.strip()
track_number = track_number.strip()
full_block = {'artist': artist_name, 'album': album_name, 'track': track_number}
return full_block
while True:
artist_name = input("Enter artist_name")
album_name = input("Enter album_name")
track_number = input("Enter album_name")
if artist_name == 'q' or album_name == 'q' or track_number == 'q':
#If even on of the inputs is q, it will break out of the while loop
print("You pressed q, quitting..")
break
else:
musician = make_album(artist_name, album_name, track_number)
print(musician)
答案 1 :(得分:1)
多亏了您,我设法找到了解决方法:)
def make_album(artist_name, album_name, track_number):
""""Returns a dictionary of an Artist"""
artist_name = artist_name.strip() #removes the spaces
album_name = album_name.strip() #removes the spaces
track_number = track_number.strip() #removes the spaces
full_block = {'artist': artist_name, 'album': album_name, 'track': track_number}
return full_block
while True: #I enter into the while loop
block_0 = input("Enter artist name: ")
if block_0 == 'q':
print("quitting...")
break
block_1 = input("Enter an album name: ")
if block_1 == 'q':
print("quitting...")
break
block_2 = input("Enter how many tracks: ")
if block_2 == 'q':
print("quitting....")
break
else:
in_full = make_album(artist_name=block_0.title(), album_name=block_1.title(), track_number=block_2.title())
print(in_full)
print("End...")
break