+ 1

What is the problem in this code?

Someone can help me with this code, the objective is to find if all the condition are true, print "d", but if one is false to print "broken". I can't find the mistake. a, b, c, d = input() .split() a = int() b = int() c = int() d = int() x = a < 10 y = b > 50 z = c > 80 if x == True and y == True and z == True: print(c) else: print("broken")

11th Jun 2021, 12:11 AM
Diana Miranda
Diana Miranda - avatar
4 odpowiedzi
+ 5
1. You're setting each variable (a,b,c,d) to an int class object without a value, which will default to 0. So all values (a,b,c,d) are equal to 0. a = int() # a == 0 Vs a = int(a) # this will convert a to an int object I.E. int() = 0 int('6') = 6 So currently, your if-else will always run the else block and output "broken". You could instead use the map function like so; a, b, c, d = map(int, input().split()) This will get the input, split it into a list and map each element to the int function converting the element to an int. Then assign them to their variable name. Your if conditions are redundant BTW. You could just as easily write it like; if x and y and z: or better yet get rid of x,y,z if a < 10 and b > 50 and c > 80: Then you say you want to output "d" if all are True, but you have; print(c)
11th Jun 2021, 1:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Calvin Thomas The split() method returns a list. map() returns a map object. You can unpack any iterable, which a map object is an iterable type. HTH
11th Jun 2021, 7:45 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg Isn't the map object converted into a tuple instead of a list for unpacking the elements? I might be wrong.
11th Jun 2021, 7:35 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
ChaoticDawg I see, thanks.
11th Jun 2021, 8:12 AM
Calvin Thomas
Calvin Thomas - avatar