Python列出了一些问题
所以我对一般的编码是新手,但我正在学习Python。所以我把这些放在了一起,但这并不是将uInput与“con”列表进行比较。我做错了什么Python列出了一些问题,python,python-3.x,Python,Python 3.x,所以我对一般的编码是新手,但我正在学习Python。所以我把这些放在了一起,但这并不是将uInput与“con”列表进行比较。我做错了什么 #countries.py con = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')] accepted = [ ] while len(accepted) < 196: print("You have ",
#countries.py
con = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')]
accepted = [ ]
while len(accepted) < 196:
print("You have ", len(accepted), "/ 196 Countries!")
uInput = input("Enter the country: ")
print("")
if uInput.lower() in con:
if uInput.lower() in accepted:
print("Already got that one!")
print("")
else:
accepted.append(uInput.lower())
print("Nice! Now for the next!")
print("")
else:
print("Country not recognised, did you spell it right?")
print("")
print("You got them all!")
#countries.py
con=[(‘英国’、‘英国’、(‘美国’、‘美国’、‘美利坚合众国’)、(‘日本’)]
已接受=[]
而len(已接受)<196:
打印(“您有”,len(已接受),“/196个国家!”)
uInput=输入(“输入国家:”)
打印(“”)
如果在con中输入uInput.lower():
如果接受中的uInput.lower():
打印(“已经得到了那个!”)
打印(“”)
其他:
accepted.append(uInput.lower())
打印(“很好!现在开始下一步!”)
打印(“”)
其他:
打印(“未识别国家,拼写正确吗?”)
打印(“”)
打印(“你都拿到了!”)
*已编辑
这是我现在更新的代码,但它不是检查副本或添加副本,你可以根据自己的意愿多次进入英国。但是因为没有任何东西被添加到可接受的列表中,分数也不会增加
#countries.py
con = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')]
accepted = [ ]
while len(accepted) < 196:
print("You have ", len(accepted), "/ 196 Countries!")
uInput = input("Enter the country: ")
print("")
foundCon = False
for conTuple in con:
if uInput.lower() in conTuple:
foundCon = True
print("Nice! Now for the next!")
print("")
...
duplicate = False
for c in accepted:
if c in conTuple:
duplicate = true
if duplicate:
print("You've already entered that one!")
...
if not foundCon:
print("Country not recognised, did you spell it right?")
print("")
print("You got them all!")
...
#countries.py
con=[(‘英国’、‘英国’、(‘美国’、‘美国’、‘美利坚合众国’)、(‘日本’)]
已接受=[]
而len(已接受)<196:
打印(“您有”,len(已接受),“/196个国家!”)
uInput=输入(“输入国家:”)
打印(“”)
foundCon=False
对于con中的偶合:
如果uInput.lower()处于偶合状态:
foundCon=True
打印(“很好!现在开始下一步!”)
打印(“”)
...
重复=错误
对于已接受的c:
如果c处于偶合状态:
重复=真
如果重复:
打印(“您已经输入了那个!”)
...
如果不是foundCon:
打印(“未识别国家,拼写正确吗?”)
打印(“”)
打印(“你都拿到了!”)
...
中的不是递归的;它可以在con
中找到('uk','uk')
,但不能找到'uk'
或'uk'
——它们都不是con
的元素
最简单(也是最好的)修复方法(仅此问题)是检查con
中的元素是否包含以下输入:
if any(uInput.lower() in c for c in con):
顺便说一句,最好是
- 只需将
uInput
转换成小写一次,然后使用它
- 对每个
con
元素以及接受的元素使用set
——语义上,set
主要用于在中查找事物(以及其他数学set
操作),并为此目的进行优化,以不保持元素顺序为代价
- 存储(并检查)整个
集合
s个国家名称在接受的范围内
;否则,我可以一次输入uk
,第二次输入ukin
,而ukin
将不会被接受
,我将获得该国的双重学分
调用input时,返回一个字符串,这意味着uInput是一个字符串
您的列表con是一个元组列表。。因此,在这一行中:
if uInput.lower() in con:
您总是会得到false,因为字符串与元组进行比较。它不是与每个元组中的每个元素进行比较,而是将元组作为一个整体进行比较。因此,uInput永远不会等于con元素
也许可以尝试将代码更改为以下内容:
foundCon = False
for conTuple in con:
if uInput.lower() in conTuple:
foundCon = True
# perform logic for matching items
...
duplicate = False
for c in accepted:
if c in conTuple:
duplicate = True
if duplicate:
# perform logic for duplicate items
...
if not foundCon:
# perform logic for no matches
...
Karl Knechtel指出了要点:“in”不会进入嵌套列表(您有一个列表,其中每个元素都是一个列表)。但如果您确实想查看列表“blah”列表中是否存在元素“foo”,则:
blah=[['hi','there'],['yo','whatsup','foo']]
if('foo' in [y for y in x for x in blah]):
print("Yep, it's in there")
基本上,通过使用列表理解,您可以快速平复“废话”,使其成为:
blah_flattened = ['hi','there','yo','whatsup']
您应该首先展平con
(最后一项除外),因为in
运算符不会在内部元组中搜索。您需要添加重复逻辑,以便它嵌套在conTuple:
语句中的ifuInput.lower()中。如果uInput有效,您只需要检查是否存在重复项。好吧,这样更有意义。但是关于集合呢,因为正如Karl在上面所说的,用户输入说uk是可以的,uk被添加到接受,但是英国仍然可以用来获取积分。那么我如何将整个元组添加到accepted中呢。所以基本上把('uk','uk')改为accepted?将其添加到我的回答中,所以我根据你的回答更改了代码,但如上所述,我仍然有问题。基本上,我在查找重复项时所做的是——如果这个国家将是重复的,重复的答案将与uInput位于同一元组中。因此,如果我在接受列表中已经有了“uk”,然后我输入了“united kingdom”,那么会发生这样的情况:1)我在con中计算元组,直到找到其中包含“united kingdom”的元组。2) 我发现匹配的偶合是('uk','uk')3)我查看每个被接受的成员,看看它是否在这个元组中。4) “uk”位于('uk'、'uk'),因此“uk”必须与之前的值“uk”重复