Python 简单的编码-需要帮助
编写一个程序,每次可以输入一个翻译对, (例如,friend=kalyardi)并被告知您有多少独特的线路 进入。你不应该计算重复项。程序应该停止 当您输入一个空行,然后打印出来时,要求输入更多的单词 你知道多少独特的翻译 例如:Python 简单的编码-需要帮助,python,Python,编写一个程序,每次可以输入一个翻译对, (例如,friend=kalyardi)并被告知您有多少独特的线路 进入。你不应该计算重复项。程序应该停止 当您输入一个空行,然后打印出来时,要求输入更多的单词 你知道多少独特的翻译 例如: Word: friend = kalyardi Word: happy = jipa-jipa Word: bird = jirripirdi Word: friend = kalyardi Word: You know 3 unique word translat
Word: friend = kalyardi
Word: happy = jipa-jipa
Word: bird = jirripirdi
Word: friend = kalyardi
Word:
You know 3 unique word translation(s)!
及
有时一个单词会有多个(或类似)翻译,其中
在这种情况下,您需要单独计算每个翻译,只需计算
唯一行的数目
我的程序是这样的-
translation = input("Word: ")
count = 0
previous = []
while translation != "":
if translation not in previous:
count = (count - 1)
translation = input("Word: ")
else:
break
print("You know", count, "unique translation(s)!")
当我运行我的程序时,它会运行
Word: bandicoot = jarlku
Word: bandicoot = jarlku
Word: dog = jarntu
Word: dog = kuna-palya
Word: kangaroo = kanyarla
Word: cockatoo = ngaarnkamarda
Word:
You know -6 unique translation(s)!
我该怎么做才能修复我的程序被卡住了好长时间
translation = input("Word: ")
previous = []
while translation != "":
source = translation.split()[0].strip()
if source not in previous:
previous.append (source)
translation = input("Word: ")
print("You know", len(previous), "unique translation(s)!")
Ashwini Chaudhary在评论中提出的另一种获取输入的酷方法
previous = []
for translation in iter(input, ""):
source = translation.split()[0].strip()
if source not in previous:
previous.append (source)
print("You know", len(previous), "unique translation(s)!")
编辑:如果必须匹配整个字符串
previous = []
for translation in iter(input, ""):
if translation not in previous:
previous.append (source)
print("You know", len(previous), "unique translation(s)!")
输出
~$ python3 Test.py
Word: friend = kalyardi
Word: happy = jipa-jipa
Word: bird = jirripirdi
Word: friend = kalyardi
Word:
You know 3 unique translation(s)!
Ashwini Chaudhary在评论中提出的另一种获取输入的酷方法
previous = []
for translation in iter(input, ""):
source = translation.split()[0].strip()
if source not in previous:
previous.append (source)
print("You know", len(previous), "unique translation(s)!")
编辑:如果必须匹配整个字符串
previous = []
for translation in iter(input, ""):
if translation not in previous:
previous.append (source)
print("You know", len(previous), "unique translation(s)!")
输出
~$ python3 Test.py
Word: friend = kalyardi
Word: happy = jipa-jipa
Word: bird = jirripirdi
Word: friend = kalyardi
Word:
You know 3 unique translation(s)!
1) 将带有input
的行移动为循环的最后一条语句
2) 在if块中,不要中断循环,将新翻译添加到previous
列表中
3) 在末尾打印
len(上一个)
。不需要count
变量。1)使用input
将行移动为循环的最后一条语句
2) 在if块中,不要中断循环,将新翻译添加到previous
列表中
3) 在末尾打印
len(上一个)
。不需要count
变量。试试这个
translation = input("Word: ")
count = 0
previous = []
while translation != "":
if translation not in previous:
count = (count - 1)
previous.append(translation)
translation = input("Word: ")
print("You know", count, "unique translation(s)!")
试试这个
translation = input("Word: ")
count = 0
previous = []
while translation != "":
if translation not in previous:
count = (count - 1)
previous.append(translation)
translation = input("Word: ")
print("You know", count, "unique translation(s)!")
你甚至没有解释你的问题。人们不必运行您的代码。好吧,只需将其包括在内。您没有使用
previous
。它总是空的。你是说(count+1)
?你已经被介绍到dict
s了吗?@Ron:然后学习一些Python教程。如果不先学习基础知识,你就不能尝试编程,这是行不通的,对不起。你甚至没有解释你的问题。人们不必运行您的代码。好吧,只需将其包括在内。您没有使用previous
。它总是空的。你是说(count+1)
?你已经被介绍到dict
s了吗?@Ron:然后学习一些Python教程。如果不先学习基础知识,你就不能尝试编程,这是行不通的,对不起。count
变量是冗余的。代码无效-它只验证源单词,因此如果翻译不明确,它将失败。@BartoszKP收到…:)while
循环可以替换为:用于iter中的翻译(输入,”):
单词:bandicoot=jarlku单词:bandicoot=jarlku单词:dog=jarntu单词:dog=kuna palya单词:kangaroo=kanyarla单词:cockato=ngaarnkamarda单词:您知道5种独特的单词翻译@AshwiniChaudhary谢谢你。了解了iter
:)count
变量是冗余的。代码无效-它只验证源单词,因此如果翻译不明确,它将失败。@BartoszKP收到…:)while
循环可以替换为:用于iter中的翻译(输入,”):
单词:bandicoot=jarlku单词:bandicoot=jarlku单词:dog=jarntu单词:dog=kuna palya单词:kangaroo=kanyarla单词:cockato=ngaarnkamarda单词:您知道5种独特的单词翻译@AshwiniChaudhary谢谢你。了解国际热核试验堆(iter)