Here is the file 'the_guessing_game':
3
2
< 100 No
> 100 No
3
< 2 Yes
> 4 Yes
= 3 No
6
< 2 Yes
> 1 Yes
= 1 Yes
= 1 Yes
> 1 Yes
= 1 Yes
Here is the python code:
infile = open("the_guessing_game")
testcases = next(infile)
print "no of testcase : %s" %(testcases)
for case in testcases:
hints = next(infile)
print "no of hints : %s" %(hints)
for hint in hints:
hint = next(infile)
print "hint : %s" %(hint)
The result of above code is:
no of testcase : 3
no of hints : 2
hint : < 100 No
hint : > 100 No
no of hints : 3
hint : < 2 Yes
hint : > 4 Yes
Why doesn't it print the hints in the range of 'no. of hints' Also, 'no. of testcases' is 3 but the loop runs just twice. What am I doing wrong?
Replace
for case in testcases:
with:
for case in range(int(testcases)):
With the former line, the loop runs once for each character in the string contained in the variable testcases. With the later line, the loop runs int(testcases)
number of times.
The string testcases consists of two characters: a three and a new line: '3\\n'
. So, in the former case, the loop runs twice, once for each character.
In some languages, like bash or tcl, a string can be used as a number, depending on context. In python, a string is a string unless you convert it, such as with int
or float
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.