My issue: The issue is when the program compares the ID to remove (remID) with the ID of the scouts currently being looked at in the file (sctID), it returns false when in fact they are equal. It may be an issue with my handling of the variables, my splitting of the line to get the ID, or even my data types. I just don't know. I tried converting both to string, but still false. The code for this section is below. Thank you in advance!
elif self._name == "rem": remID = str(scoutID.get()) if remID != "": #store all the lines that are in the file in a temp file with open(fileName, "r") as f: with open(tempFileName, "a") as ft: lines = f.readlines() for line in lines: sctID = str(line.split(",")[3]) print("%s,%s,%s" % (remID, sctID, remID == sctID)) #print(remID) if sctID != remID: #if the ID we are looking to remove isn 't #the ID of the scout we are currently looking at, move it to the temp file ft.write(line) #remove the main file, then rectrate a new one os.remove(fileName) file = open(fileName, "a") file.close() #copy all the lines back to the main file with open(tempFileName, "r") as tf: lines = tf.readlines() with open(fileName, "a") as f: for line in lines: f.write(line) #finally, delete and recreate the temp file os.remove(tempFileName) file = open(tempFileName, "a") file.close() #remove the window master.destroy()
My output:
1, 1, False 1, 2, False 1, 3, False
This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case, pass a code object instead of a string. If the code object has been compiled with 'exec' as the mode argument, eval()'s return value will be None.,Raises an auditing event exec with the code object as the argument. Code compilation events may also be raised.,This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (x in this case.),If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.
def all(iterable):
for element in iterable:
if not element:
return False
return True
def any(iterable):
for element in iterable:
if element:
return True
return False
>>> bin(3)
'0b11' >>>
bin(-10)
'-0b1010'
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110') >>>
f '{14:#b}', f '{14:b}'
('0b1110', '1110')
class C:
@classmethod
def f(cls, arg1, arg2): ...
>>> import struct >>> dir() # show the names in the module namespace['__builtins__', '__name__', 'struct'] >>> dir(struct) # show the names in the struct module['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from'] >>> class Shape: ...def __dir__(self): ... return ['area', 'perimeter', 'location'] >>> s = Shape() >>> dir(s)['area', 'location', 'perimeter']
It may seem odd that Python distinguishes the integer value 1 from the floating-point value 1.0. They may represent the same number, but they belong to different types. The reason is that they are represented differently inside the computer.,int can also convert floating-point values to integers, but remember that it truncates the fractional part:,Each Python type comes with a built-in command that attempts to convert values of another type into that type. The int(ARGUMENT) command, for example, takes any value and converts it to an integer, if possible, or complains otherwise:,If we expect the response to be an integer, we can use the input function which evaluates the response as a Python expression:
>>> quotient = 7 / 3 >>> print quotient 2 >>> remainder = 7 % 3 >>> print remainder 1
>>> type(True)
<type 'bool'>
>>> type(true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> 5 == 5 True >>> 5 == 6 False
x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y
if x > 0:
print "x is positive"
if BOOLEAN EXPRESSION: STATEMENTS
It may seem odd that Python distinguishes the integer value 1 from the floating-point value 1.0. They may represent the same number, but they belong to different types. The reason is that they are represented differently inside the computer.,int can also convert floating-point values to integers, but remember that it truncates the fractional part:,Programs get really interesting when we can test conditions and change the program behaviour depending on the outcome of the tests. That’s what this chapter is about.,The float function converts integers and strings to floating-point numbers:
>>> type(True)
<class 'bool'>
>>> type(true)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
NameError: name 'true' is not defined
>>> 5 == (3 + 2) # Is five equal 5 to the result of 3 + 2 ? True >>> 5 == 6 False >>> j = "hel" >>> j + "lo" == "hello" True
x == y # Produce True if...x is equal to y x != y #...x is not equal to y x > y #...x is greater than y x < y #...x is less than y x >= y #...x is greater than or equal to y x <= y #...x is less than or equal to y
>>> age = 18
>>> old_enough_to_get_driving_licence = age >= 17
>>> print(old_enough_to_get_driving_licence)
True
>>> type(old_enough_to_get_driving_licence)
<class 'bool'>
n * 0 == 0
x and False == False False and x == False y and x == x and y x and True == x True and x == x x and x == x
The any() function returns True if any element of an iterable is True. If not, it returns False.,In the case of dictionaries, if all keys (not values) are false or the dictionary is empty, any() returns False. If at least one key is true, any() returns True.,False if all elements are false or if an iterable is empty,True if at least one element of an iterable is true
Example
boolean_list = ['True', 'False', 'True'] # check if any element is true result = any(boolean_list) print(result) # Output: True
Example
boolean_list = ['True', 'False', 'True'] # check if any element is true result = any(boolean_list) print(result) # Output: True
The syntax of any()
is:
any(iterable)
Example 1: Using any() on Python Lists
# True since 1, 3 and 4(at least one) is true l = [1, 3, 4, 0] print(any(l)) # False since both are False l = [0, False] print(any(l)) # True since 5 is true l = [0, False, 5] print(any(l)) # False since iterable is empty l = [] print(any(l))
Example 2: Using any() on Python Strings
# At east one(in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))
In the case of dictionaries, if all keys (not values) are false or the dictionary is empty, any()
returns False
. If at least one key is true, any()
returns True
.
# 0 is False d = { 0: 'False' } print(any(d)) # 1 is True d = { 0: 'False', 1: 'True' } print(any(d)) # 0 and False are false d = { 0: 'False', False: 0 } print(any(d)) # iterable is empty d = {} print(any(d)) # 0 is False # '0' is True d = { '0': 'False' } print(any(d))
Last Updated : 15 Sep, 2021,GATE CS 2021 Syllabus
Output:
False True False False False False False True
Output:
Are you hungry ? True or false : False
Let 's go for walk
def letter_check(word, letter): for character in word: if character == letter: return True else: return False,Checks the first character of the word. a. if its the same as the letter, it returns True. b. if its not, it returns False.,Because when you do character == letter, it will only check the first character. It has to do with understanding return statements. When you have a return statement the loop will stop. For example: if you are checking if the letter “m” is in ice-cream, your code will do this:,Thanks for your answer sir. Unfortunately, I still didn’t quite understand. Does it exit the function after checking if character == letter and thus deciding if returning True or not returning True? If so, does it mean that it doesn’t even consider returning False (in a code written this way)?
Why in the context of this exercise the following code isn’t correct:
def letter_check(word, letter):
for character in word:
if character == letter:
return True
else:
return False
The answers above are absolutely correct, but I thought I will word it the way I understood, as I wrote the exact same code first and amended it after encountering incorrect output.
> def letter_check(word, letter):
>
for character in word:
>
if character == letter:
>
return True >
else:
>
return False
def letter_check(word, letter):
for character in word:
if character == letter:
return True
return False
see I would have written this instead
def letter_check(word, letter):
for character in word:
if character == letter:
return True
for character in word:
if character != letter:
return False
If a match is found the function is exited with a True return.
for character in word:
if character != letter:
return False
Here are some examples:
def vowel_check(word): '' 'return True if word contains any vowel, otherwise return False '' ' for character in word: if character in 'aeiou': return True return False def vowel_count(word): '' 'return the number of vowels contained in word '' ' count = 0 for character in word: if character in 'aeiou': count += 1 return count print(vowel_check('aardvark')) print(vowel_count('aardvark')) print(vowel_check('psst')) print(vowel_count('psst'))
Output:
True 3 False 0