编程问题的新手 [英] New to programming question

查看:62
本文介绍了编程问题的新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自非程序员Python教程的练习

作者:Josh Cogliati。


练习是:

编写一个程序,用户猜出你的名字,但他们只有3

的机会这样做,直到程序退出。


这是我的剧本:


--------------------------

count = 0

name = raw_input(猜猜我的名字。)


而名字!=''本''和计数< 3:

count = count + 1


if name!=''Ben''和count< 3:

name = raw_input(''再猜一次。'')

elif name ==''Ben''和count< 3:

打印你是对的!

否则:

打印''不再尝试。''


----------------------------------


一切都有效除了线:打印你是对的!


有人能告诉我出了什么问题并给我一个更好的选择

我想出了什么。


谢谢




This is an exercise from the Non-programmers tutorial for Python
by Josh Cogliati.

The exercise is:

Write a program that has a user guess your name, but they only get 3
chances to do so until the program quits.

Here is my script:

--------------------------

count = 0
name = raw_input("Guess my name. ")

while name != ''Ben'' and count < 3:
count = count + 1

if name != ''Ben'' and count < 3:
name = raw_input(''Guess again. '')
elif name == ''Ben'' and count < 3:
print "You''re right!"
else:
print ''No more tries.''

----------------------------------

Everything works except the line: print "You''re right!"

Could someone tell me what is wrong and give me a better alternative to
what I came up with.

Thank you

Ben

推荐答案

Ben写道:
这是来自非程序员的Python教程的练习
作者:Josh Cogliati。

练习是:

编写一个让用户猜出你的名字的程序,但只有3个机会这样做才能退出程序。
<这是我的剧本:

--------------------------

count = 0
name = raw_input(猜猜我的名字。)

而名字!=''Ben''和count< 3:
count = count + 1

如果名字!=''本''和计数< 3:
name = raw_input(''再猜一次。'')
elif name ==''Ben''和count< 3:
打印你是对的!
其他:
打印''不再尝试。''

------ ----------------------------

一切都有效除了线:打印你是对的!

有人能告诉我出了什么问题


while循环中的代码(即缩进的所有内容)仅在
时执行
while条件为真,即name!= Ben和count< 3


所以姓名==''Ben'':永远是假的,你是对的!将永远不会打印


并给我一个更好的替代方案。

只是一个提示:如果你分开,你可能会找到一个更清洁的解决方案测试名称

来自计数测试。


谢谢

This is an exercise from the Non-programmers tutorial for Python
by Josh Cogliati.

The exercise is:

Write a program that has a user guess your name, but they only get 3
chances to do so until the program quits.

Here is my script:

--------------------------

count = 0
name = raw_input("Guess my name. ")

while name != ''Ben'' and count < 3:
count = count + 1

if name != ''Ben'' and count < 3:
name = raw_input(''Guess again. '')
elif name == ''Ben'' and count < 3:
print "You''re right!"
else:
print ''No more tries.''

----------------------------------

Everything works except the line: print "You''re right!"

Could someone tell me what is wrong
The code within the while loop (i.e., everything indented) is executed only if
the while condition is true, i.e., name != Ben and count < 3

So name == ''Ben'': will always be false and "You''re right!" will never get printed

and give me a better alternative to what I came up with.
Just a hint: you may find a cleaner solution if you separate the tests for name
from the test for count.

Thank you

Ben



HTH


Michael


HTH

Michael


2005年3月31日20:03:00 -0800,"本" <是********* @ gmail.com>写道:
On 31 Mar 2005 20:03:00 -0800, "Ben" <be*********@gmail.com> wrote:
有人可以告诉我出了什么问题,并给我一个更好的选择
我想出了什么。
Could someone tell me what is wrong and give me a better alternative to
what I came up with.




从测试中分离原始输入语句。你的elsif是跳过它的



在你的

语句之后立即尝试只使用一个原始输入语句。 />

Ron



Seperate you raw input statements from your test. Your elsif is
skipping over it.

Try using only one raw imput statement right after your while
statement.

Ron


Ben写道:
这是非程序员的练习Python教程由Josh Cogliati撰写。

练习是:

编写一个程序,用户猜出你的名字,但他们只得到3 机会退出之前有机会这样做。

这是我的剧本:

------------------- -------

count = 0
name = raw_input(猜猜我的名字。)

同时名字!=''本''和计数< 3:


此循环内的所有内容只会在名称不等于

''Ben'且计数小于3时出现。

count = count + 1


你将计数增加一,这样可以让你的代码抓住案件

where count = 2,现在等于3.

如果名字!=''本''和计数< 3:
name = raw_input(''再猜一次。'')
elif name ==''Ben''和count< 3:
打印你是对的!
否则:
打印''不再尝试。''


这是为什么你得到这个打印信息,因为数量现在等于3.

--------------------------- -------

一切都有效除了线:打印你是对的!


但是程序没有机会打印不再需要
尝试。因为这个循环里面没有任何意义==''Ben''。

有人可以告诉我出了什么问题,给我一个更好的选择
我想出了什么。

谢谢

This is an exercise from the Non-programmers tutorial for Python
by Josh Cogliati.

The exercise is:

Write a program that has a user guess your name, but they only get 3
chances to do so until the program quits.

Here is my script:

--------------------------

count = 0
name = raw_input("Guess my name. ")

while name != ''Ben'' and count < 3:
Everything inside this loop will only occur if the name doesn''t equal
''Ben'' and the count is less than 3.
count = count + 1
You increase the count by one, which allows your code to catch the case
where count = 2 and now equals 3.
if name != ''Ben'' and count < 3:
name = raw_input(''Guess again. '')
elif name == ''Ben'' and count < 3:
print "You''re right!"
else:
print ''No more tries.''
Which is why you get this print message, because count is now equal to 3.

----------------------------------

Everything works except the line: print "You''re right!"
But at no point does the program get an opportunity to print "No more
tries.'' because there is no point inside this loop where name == ''Ben''.
Could someone tell me what is wrong and give me a better alternative to
what I came up with.

Thank you

Ben




另外,你复制了大量的案例测试。您检查是否

名称在开始时是''Ben',然后在循环内,并且计数相同




我试图写出一个解决这个问题的逻辑方法,但实际上这个特殊的用例并不是那么简单吗?


这里是我的贡献:


count = 0

#获取第一个输入

name = raw_input("猜我的名字:")

#给傻逼两个额外的东西

而count< 2:

#检查名称值

如果名称==''本'':

打印你是对的!"

break

else:

name = raw_input("再试一次:")

#当然,我们还没有检查过吸盘的最后一次猜测

#所以我们现在必须这样做。

if count == 2:

如果名字==''本'':

打印你是对的!

其他:

打印不再为你试试!!!

希望这会有所帮助。

Joal



Also, you''re duplicating a lot of your case testing. You check to see if
the name is ''Ben'' at the start, and then inside the loop, and the same
for the counts.

I tried to write out a logical method of approaching this problem, but
in truth this particular use-case isn''t that simple is it?

Here''s my contribution anycase:

count = 0
# Get first input
name = raw_input("Guess my name: ")
# Give the sucker two extra goes
while count < 2:
# Check the value of name
if name == ''Ben'':
print "You''re right!"
break
else:
name = raw_input("Try again: ")
# Of course, we haven''t checked the sucker''s last guess
# so we have to do that now.
if count == 2:
if name == ''Ben'':
print "You''re right!"
else:
print "No more tries for you!!!"
Hope this helps.
Joal


这篇关于编程问题的新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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