0

Homework question is asking me to write a program that would output True if an integer is odd and has the number "0" in the middle of it. I figured out how to get it to print True if a number is odd but can't figure out how to detect if the number 0 is in the middle.

I've figured out the first condition which would be detecting if it's odd.

input: def is_cyclops(n): if len(str (n)) % 2 != 0: return True return False print (is_cyclops(11011)) output: True 

I want to know how to get the code to detect the number 0 in the middle.

2 Answers 2

3

I'll provide a response in the form of an algorithm:

  • Convert the number to a string
  • Detect whether the string has an even or odd number of characters (because even numbered strings don't have a single "middle" character)
  • Look at the middle character which is character # (len(str)/2)-0.5
  • That's your middle character
Sign up to request clarification or add additional context in comments.

2 Comments

Since ((len(n)/2)-0.5) is supposed to find the middle character, wouldnt the logical thing to do would be making an if statement where it would return True if ((len(n)/2)-0.5) == 0? for some reason I get an "None" returned.
@FarhanIqbal That's an alternative way of handling what my second bulletpoint item covers.
0

This code will work for an input n.

n = str(n) if(len(n)%2==0): #Checks if even if(n[len(n)/2]==0): #Checks for presence of 0 return True else: if(n[len(n+1)/2]==0): #Checks for presence of 0 return True 

1 Comment

Hey Vishwan, Thank you for the reply but for some reason whenever I try to run the code it gives me an error which says: if(n[len(n+1)/2]==0): (len(str)/2)-0.5) == 0):TypeError: object of type 'int' ha s no len()s no len()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.