首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的程序多次引用readline命令?如何停止它?

为什么我的程序多次引用readline命令?如何停止它?
EN

Stack Overflow用户
提问于 2018-03-27 06:12:54
回答 1查看 25关注 0票数 0

好吧。所以我正在试着写一个代码,基本上就是猜词游戏。因为它是针对学校作业的,所以有一些必要的部分,比如4个函数的使用和那些做某些事情的函数。该程序需要从存储在外部.txt文件中的单词表中提取信息。当我尝试使用readline命令中的一行时,每次我引用该函数时,它都会移动到下一行,这让我很为难。

以下是代码

代码语言:javascript
运行
复制
import random
#Variables
file = open('words.txt','r')
Number_of_lines = 0
Correct = 'Place holder'
Score = 0
#Retrieve the next word through readline command
def get_a_Word():
    Basic_Word = file.readline()
    Word = Basic_Word
    Word = Word
    return Word
#Turn the word into a guess word
def guess_Word():
    Word = get_a_Word()
    Edited_Word = '*' + Word[1:]
    return Edited_Word
def check_Word(Word):
    if Word == get_a_Word():
        return True
    else:
        return False
#Put that shit together
def Main():
    Line = 0
    while Line < 10:
        Edited_Word = guess_Word()
        Score = 0
        Line = Line + 1
        Word = input('Given {} What is the word? '.format(Edited_Word))
        Word = Word.upper()
        if check_Word(Word) == True:
            print('That is correct!')
            Score = Score + 10
        elif check_Word(Word) == False:
            print('That is incorrect. the word was {}.'.format(get_a_Word()))
        else:
            print('you broke it')
    Correct = Score/10
   print('You have successfully guessed {} out of 10 words. Your final score is {}.' .format(Correct, Score))
Main()
file.close()

.txt文件包含这些单词的顺序商店苹果自行车水梅赛德斯教室建筑师电梯测量哥斯拉

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2018-03-27 06:40:42

我不知道您应该拥有的函数是如何指定的,但是不获取多个不同单词的明显解决方案是在主循环的每个周期中不超过一次调用get_a_Word。其他一些函数可能需要更改,以便将先前获取的单词作为参数。

这个循环看起来像这样(伪代码,我可能跳过了一些东西):

代码语言:javascript
运行
复制
while line < 10:
    word = get_a_Word()
    edited_word = guess_Word(word)
    guess = input('Given {} What is the word? '.format(edited_word))
    if check_Word(word, guess):
        print('That is correct!')
        score += 10
    else:
        print('That is incorrect. The word was {}.'.format(word))

请注意与您的问题无关的命名问题: Python命名变量和函数的约定是将lowercase_names_with_underscores用于所有内容,除非代码模仿的是使用不同约定的现有API。对类使用CapitalizedNames,对常量使用ALL_CAPS

然而,最重要的事情是保持一致。您当前的代码似乎混合了下划线、大写和其他样式,没有任何逻辑。选择一种风格(即使它不是我在上一段中描述的风格)并坚持下去。(如果讲师的命名风格不一致,则很难做到这一点。可惜,对此您可能无能为力。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49501459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档