试图锤击这个zylabs实验室,但仍在挣扎 [英] Trying to hammer out this zylabs Lab but struggling

查看:116
本文介绍了试图锤击这个zylabs实验室,但仍在挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个实验室上花了几天的时间,我只是在努力获得适当的输出.如果有人可以告诉我该怎么做,或者您可以共享代码,那么我可以对它进行反向工程.感谢您的帮助!

I've spent a couple of days on this Lab and I'm just struggling to get appropriate outputs. If someone can show me where to go with this, or if you have the code itself to share, I can reverse-engineer that. Thanks for any help!

6.6热身:解析字符串(Python 3)

6.6 Warm up: Parsing strings (Python 3)

(1)提示用户输入一个包含两个用逗号分隔的字符串的字符串. (1分)

(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt)

可以接受的字符串示例:

Examples of strings that can be accepted:

  • Jill, Allen
  • Jill , Allen
  • Jill,Allen
  • Jill, Allen
  • Jill , Allen
  • Jill,Allen

例如:

Enter input string: Jill, Allen

(2)如果输入字符串不包含逗号,则报告错误.继续提示直到输入有效的字符串.注意:如果输入包含逗号,则假定输入也包含两个字符串. (2分)

(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)

例如:

Enter input string: Jill Allen
Error: No comma in string.
Enter input string: Jill, Allen

(3)使用字符串拆分,从输入字符串中提取两个单词,然后删除所有空格.输出两个词. (2分)

(3) Using string splitting, extract the two words from the input string and then remove any spaces. Output the two words. (2 pts)

例如:

Enter input string: Jill, Allen
First word: Jill
Second word: Allen

(4)使用循环,扩展程序以处理多行输入.继续直到用户输入q退出. (2分)

(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)

例如:

Enter input string: Jill, Allen
First word: Jill
Second word: Allen


Enter input string: Golden , Monkey
First word: Golden
Second word: Monkey

Enter input string: Washington,DC
First word: Washington
Second word: DC

Enter input string: q

推荐答案

我知道您正在学习Python.每个人都应该知道,这太好了!

Hi I see you are learning Python. Everyone should know it, it's great!

鉴于这些在某种程度上被认为是Python的基础知识,我将为您提供学习如何解决此问题的资源.

Given that these are somewhat considered Python basics, I shall provide you with the resources to learn how to solve this yourself.

如果给出了答案,那么您将不会学习这种精彩的语言,请允许我通过有用的链接和说明来扩展您的实验单:

If you are given the answer then you won't learn this wonderful languageso let me ammend your labsheet with helpful links and instructions:

(1)提示用户输入一个包含两个用逗号分隔的字符串的字符串. (1分)

(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt)

您需要字符串用户输入.在此旅游网站上查看raw_input()函数,不要忘记存储函数的返回值.

You need string user input. Look at the raw_input() function on this turorial site, don't forget to store the return value of the function.

(2)如果输入字符串不包含逗号,则报告错误.

(2) Report an error if the input string does not contain a comma.

您需要检查一个字符串是否包含另一个字符串,请检查此这样的答案并存储为布尔值(为此答案叫inputHasComma)

You need to check if a string contains another string, check this SO answer and store it as a boolean (for this answer lets call it inputHasComma)

继续提示直到输入有效的字符串.注意:如果输入包含逗号,则假定输入也包含两个字符串. (2分)

Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)

您将需要一个while循环,这是一个有关while循环的教程,使条件使用inputHasComma(您之前定义的).

You will need a while loop, here is a tutorial on while loops, make the condition use inputHasComma (that you defined earlier).

(3)使用字符串拆分,从输入字符串中提取两个单词,然后删除所有空格.输出两个词. (2分)

(3) Using string splitting, extract the two words from the input string and then remove any spaces. Output the two words. (2 pts)

要拆分字符串,请这是一个很好的示例.将拆分结果存储到变量(应该是列表)中,然后从列表中获取所需的元素,如下所示:

To split a string, here is a nice example. Store the result of the split into a variable (it should be a list) and then get the elements that you want out of the list like so:

values = ['A', 'B', 'C', 'D', 'E']
values[0] # returns 'A'
values[2] # returns 'C'

(4)使用循环,扩展程序以处理多行输入.继续直到用户输入q退出. (2分)

(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)

在检查使用break的逗号之前做一个if条件(在break上的教程

Do an if condition before you check for commas that uses a break (tutorial on break here). Look at the first example for an if condition that checks if a string equals another string on this tutorial site.

这应该使您通过实验室!祝你好运. :)

This should get you through the lab! Good luck. :)

这篇关于试图锤击这个zylabs实验室,但仍在挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆