在这个程序中,如果假设计算机选择单词“car”并且用户输入'c'3次,那么答案显示正确如何纠正?
import random
print("\t\t\t Welcome to 'The Advanced Jumble Game'")
print("In this program computer will choose a word and you have to guess it in 10 trails")
set=("battery","car","telephone")
choose=random.choice(set)
correct=choose
count=0
corr_length=0
print"The word contains",len(choose),"letters"
while(count<=10):
guess=raw_input("Guess a letter\n")
count=count+1;
if guess in choose:
print"Yes it's in the string"
corr_length=corr_length+1
else:
print"No it's not in the string"
if(corr_length==len(correct)):
print("You guessed it correctly")
break;
exit=raw_input("press Enter to exit")
答案 0 :(得分:2)
在if语句中,您只检查猜测的字母是否在字符串中,而不是它的索引。因此,当您输入三次c时,它实际上是正确的,因为字母c出现在字符串中。尝试在while循环中包含另一个计数器,当正确猜出字母时,该计数器会增加1。然后检查下一个猜测在计数器的索引处是否相等。不确定这是否是解决问题的最有效方法,但这是我的解决方案:
import random
print("\t\t\t Welcome to 'The Advanced Jumble Game'")
print("In this program computer will choose a word and you have to guess it in 10 trails")
set=("battery","car","telephone")
choose=random.choice(set)
correct=choose
count=0
corr_length=0
index = 0
print"The word contains",len(choose),"letters"
while(count<=10):
guess=raw_input("Guess a letter\n")
count=count+1;
print correct[index]
if guess == correct[index]:
print"Yes, correct letter chosen"
corr_length=corr_length+1
index += 1
else:
print"No it's not in the string"
if(corr_length==len(correct)):
print("You guessed it correctly")
break;
exit=raw_input("press Enter to exit")
答案 1 :(得分:0)
这是因为您正在计算用户在单词中输入字母的次数。
但是&#34; c&#34;每次都在车里,所以:
他们赢了!
你从来没有实现问题陈述的一部分,用户实际上输入了他们对该词是什么的猜测。