5 Replies - 582 Views - Last Post: 25 March 2013 - 04:31 AM Rate Topic: -----

#1 ZkP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 12

"If" Statement problem

Posted 23 March 2013 - 06:04 PM

In python, I made a function that works OK except for one thing, after it does one of the "if" statements, it goes on to the next lines to do the next if statement. Here is my code:
def circ():
    t = input('R(radius) or D(diameter)?')
    if t == 'r' or 'R':
        rc = int(input('Radius: '))
        print('Circumference of the circle is', rc*2*3.141592)
    if t == 'd' or 'D':
        dc = int(input('Diameter: '))
        print('Circumference of the circle is', dc*3.141592)


Any suggestions? And yes, I'm not a pro at this, I just started 3 days ago.

Is This A Good Question/Topic? 0
  • +

Replies To: "If" Statement problem

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: "If" Statement problem

Posted 23 March 2013 - 06:37 PM

Change the second if to elif and the second block will only execute if the first block didn't execute.
def circ():
    t = input('R(radius) or D(diameter)?')
    if t == 'r' or 'R':
        rc = int(input('Radius: '))
        print('Circumference of the circle is', rc*2*3.141592)
    elif t == 'd' or 'D':
        dc = int(input('Diameter: '))
        print('Circumference of the circle is', dc*3.141592)

This post has been edited by atraub: 23 March 2013 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,233
  • Joined: 21-June 11

Re: "If" Statement problem

Posted 24 March 2013 - 03:25 AM

In Python you can use any type of value as an condition (not just True and False). Any "null-valued" value (0, None, empty strings, empty lists, etc.) will be treated the same way as False in an if condition. Anything else will be treated as True.

or is an operator that takes two conditions as operands. If both conditions are false, the result is also false. If at least one of the conditions is true, the result is also true (specifically it's the value of the first non-false operand).

When you write t == 'd' or 'D' the two conditions are t == 'd' and 'D'. The value of t == 'd' might be True or False. The value of 'D' will always be 'D' and, as described in the first paragraph, that's a true value because it's not empty. So the whole condition will always be true because one of the ORed conditions, 'D', will always be true.

To achieve what you intended to do, use t == 'd' or t == 'D', so that both operands of or are complete conditions that can be true or false.

Better yet: convert t to lower case using the lower() method. Then you only have to compare against lower case letters and don't need any ors at all.
Was This Post Helpful? 1
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: "If" Statement problem

Posted 24 March 2013 - 11:52 PM

oh yeah, I wasn't paying attention. Good catch
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: "If" Statement problem

Posted 25 March 2013 - 04:25 AM

Two things to add. Use variables instead of magic numbers. If you're going to be using PI a lot, define it somewhere. Also, for pythony fun, get used to using "in".

e.g.
PI = 3.1415926535897931

def showCircumference():
	# t?  WTF does t mean?  Use names that make sense later
	# t = input('R(radius) or D(diameter)?')
	choice  = input('R(radius) or D(diameter)?')
	
	if choice in ('r','R'):
		# you actually don't want int, you want float
		# think about it
		# value = int(input('Radius: '))
		value = float(input('Radius: '))
		
		# don't do this twice
		# print('Circumference of the circle is', rc*2*3.141592)
		result = value * 2 * PI
		
	elif choice in ('d','D'):
		value = float(input('Diameter: '))
		result = value * PI
		
	else:
		# make sure all paths take you somewhere
		print('Invalid choice')
		result = 'Unknown'
		
	print('Circumference of the circle is', result)



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,233
  • Joined: 21-June 11

Re: "If" Statement problem

Posted 25 March 2013 - 04:31 AM

View Postbaavgai, on 25 March 2013 - 12:25 PM, said:

If you're going to be using PI a lot, define it somewhere.


No, use the pi that's already defined:

import math

... rc * 2 * math.pi ...


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1