【发布时间】:2020-11-28 09:42:03
【问题描述】:
我正在练习我的 Python 技能。我目前在第 4 周,坚持做一项特定的任务。如果用户在我的 self.songs 输入中两次输入相同的字符串,我想打印一条消息“你不能两次输入同一首歌曲。再试一次”。
我该怎么做?另外,有没有办法让用户在输入他们喜欢的歌曲时使用逗号而不是空格来分隔每个字符串?
class User:
def __init__(self):
self.name = ''
self.songs = ()
self.movies = ()
self.sports = ()
def tops(self):
self.name = input("What's you're full name?:")
while True:
self.songs = input('Hi ' + self.name + ', what is your top 5 favorite songs?:').split()
if len(self.songs) > 5:
print('You entered more than 5 songs. Try again')
elif len(self.songs) < 5:
print('You entered less than 5 songs. Try again')
elif len(self.songs) == 5:
for song in self.songs:
print(song)
confirm_input = input('Do you confirm?')
if confirm_input == 'Yes':
print(type(self.songs))
print(self.songs)
break
elif confirm_input == 'No':
continue
quizUser = User()
quizUser.tops()
【问题讨论】:
-
confirm_input = input('Type Yes to confirm'),所以,用户知道输入Yes而不是通常的y:) -
不要批评问题或答案。然而,有时采取不同的方法可能会很有用。如果您重构以循环询问标题直到 5,您可以通过签入列表轻松实现不重复。另外,您可以输入You Can't Always Get What You Want,目前这里是不可能的。
标签: python python-3.x