[英]Trouble understanding what * (asterisk) does in Python variable-assignment
I'm new to programming and am currently working through "Python Programming: An Introduction to Computer Science,2nd ed" by Zelle. 我是编程的新手,目前正在学习Zelle撰写的“ Python编程:计算机科学入门,第二版”。
While working on one of the exercises in the book,I encountered some trouble understanding a solution provided by the author. 在编写本书中的一项练习时,在理解作者提供的解决方案时遇到了一些麻烦。 The exercise is basically to make a program that gives a letter grade for a certain range of points.
练习基本上是制作一个程序,该程序在一定范围内给出字母等级。
The question is as follows: "A certain CS professor gives 100-points exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-698:D, <60:F. Write a program that accepts an exam score as input and prints out the corresponding grade." 问题如下:“某位CS教授给出了100分的考试,其等级分为90-100:A,80-89:B,70-79:C,60-698:D,<60:F 。写一个接受考试成绩作为输入并打印出相应成绩的程序。”
Here is my own source code for the exercise: 这是我自己的练习源代码:
score = float(input("Enter your quiz score: "))
if score >= 90:
print("You got an A.")
elif score >= 80:
print("You got a B.")
elif score >= 70:
print("You got a C.")
elif score >= 60:
print("You got a D.")
else:
print("You got a F.")
And it works perfectly well and from my searches,is a standard solution to such a problem. 而且它运行得很好,从我的搜索来看,它是解决此问题的标准方法。
Then,the author's solution is as follows: 然后,作者的解决方案如下:
score = eval(input("Enter the score (out of 100): "))
grades = 60*"F"+10*"D"+10*"C"+10*"B"+11*"A"
print("The grade is", grades[score])
Which I found to be so much neater as the entire if-elif-else chunk could be much more succinctly expressed with only 2 lines. 我发现它是如此整洁,因为整个if-elif-else块可以仅用2行来更简洁地表示。 However,I'm finding trouble trying to understand the 2nd line of his code: grades = 60*"F"+10*"D"+10*"C"+10*"B"+11*"A" How does this line work exactly and what does the * do in the case of a variable assignment such as this?
但是,我在尝试理解他的代码的第二行时遇到了麻烦:等级= 60 *“ F” + 10 *“ D” + 10 *“ C” + 10 *“ B” + 11 *“ A”这条线准确地工作,并且在诸如此类的变量分配的情况下,*会做什么?
Pardon me if there's already a similar question to this that answers my query,but the closest I could find was about what * does in parameters. 请问是否已经有类似的问题可以回答我的查询,但是我能找到的最接近的是*在参数中的作用。 I would gladly appreciate a link to be directed there if that's that case.
如果那样的话,我将很高兴看到指向该链接的链接。
Thanks for the help! 谢谢您的帮助!
This isn't really anything to do with variable assignment. 这实际上与变量分配无关。 In Python, you can multiply a string by a non-negative integer;
在Python中,您可以将字符串乘以非负整数。 the effect is to repeat the string the appropriate number of times.
效果是将字符串重复适当的次数。 So, eg, 5*"A"+2*"B" is "AAAAABB".
因此,例如5 *“ A” + 2 *“ B”是“ AAAAABB”。
(So in the actual code you're looking at, you have 60 "F"s -- so grades[score]
will be "F" when 0 <= score < 60 -- and then 10 "D"s -- so grades[score]
will be "D" when 60 <= score < 70 -- and so on.) (因此,在您要查看的实际代码中,您有60个“ F”-因此,当0 <=分数<60时,
grades[score]
将为“ F”,然后是10个“ D”-因此当60 <=分数<70时, grades[score]
将为“ D”,依此类推。)
It has nothing to do with assignment. 它与分配无关。
3*"F"
is "FFF"
, as simple as that. 3*"F"
就是"FFF"
,就这么简单。 "FFF"+"DDD"
is "FFFDDD"
. "FFF"+"DDD"
为"FFFDDD"
。 grades
is thus a string of 101 characters (sixty F's, ten D's... one for each score between 0
and 100
), and you just pick the right one using grades[score]
. 因此,
grades
是由101个字符组成的字符串(60个F,10个D ...每个0
至100
之间的分数对应一个),您只需使用grades[score]
选择正确的字符即可。
The asterisk *
will concatenate X versions of the preceding string, Where X is defined by the following number. 星号
*
将连接前一个字符串的X版本,其中X由以下数字定义。 The +
will concatenate the preceding and following string +
将连接前面和后面的字符串
>>> "x"*2
'xx'
>>> "x"*2+"y"*2
'xxyy'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.