# "If" Statement problem

Page 1 of 1

## 5 Replies - 709 Views - Last Post: 25 March 2013 - 04:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316481&amp;s=44f23c80e039ca77e51e39d14eb14ee6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ZkP

• New D.I.C Head

Reputation: 0
• 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

• Pythoneer

Reputation: 803
• Posts: 2,142
• 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

### #3 sepp2k

• D.I.C Lover

Reputation: 2200
• Posts: 3,393
• 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.

### #4 atraub

• Pythoneer

Reputation: 803
• Posts: 2,142
• Joined: 23-December 08

## Re: "If" Statement problem

Posted 24 March 2013 - 11:52 PM

oh yeah, I wasn't paying attention. Good catch

### #5 baavgai

• Dreaming Coder

Reputation: 6080
• Posts: 13,167
• 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.

### #6 sepp2k

• D.I.C Lover

Reputation: 2200
• Posts: 3,393
• Joined: 21-June 11

## Re: "If" Statement problem

Posted 25 March 2013 - 04:31 AM

baavgai, 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 ...

```