这是一个家庭作业问题,它要求“选择一个单词。要求用户输入一个字母。检查该字母在您的单词中出现的次数并在屏幕上输出数字。” 到目前为止,我已经按原样编写了它,它似乎工作正常:
word = str("python")
letters = len(word)
attempt = str(raw_input ("Enter a letter: "))
while attempt in word:
count = "python".count(attempt)
if attempt in word:
print "This letter is present ",count, "time(s)."
attempt = str(raw_input ("Enter another letter: "))
while attempt not in word:
attempt = str(raw_input ("This letter is not present, enter another letter: "))
count = "python".count(attempt)
if attempt in word:
print "This letter is present ",count, "time(s)."
但有时如果我输入字母,程序会停止并且不再接受任何输入。我究竟做错了什么?如果代码非常草率且写得不好,我深表歉意,因为我是编程新手。谢谢。