# Finding a number? If statement?

Page 1 of 1

## 5 Replies - 775 Views - Last Post: 24 October 2014 - 03:14 PMRate 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=356547&amp;s=a5e09cc9bfc14d384e946e7ee0657ce5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 puck2020

Reputation: 2
• Posts: 24
• Joined: 30-August 14

# Finding a number? If statement?

Posted 24 October 2014 - 01:38 PM

When the code is run it should look to see if the number #7 has been input. I'm lost. I think I need an if state??? Not sure how to do it though...

def main():

sum = 0.0
count = 0
num = []
xStr = input("Enter a number (<Enter> to quit) >> ")
while xStr != "":
x = eval(xStr)
num.append(x)
sum = sum + x
count = count + 1
xStr = input("Enter a number (<Enter> to quit) >> ")
num.sort()
print("You have entered", count, "number of numbers. These numbers are listed in ascending order:", num)

forgot main()

def main():

sum = 0.0
count = 0
num = []
xStr = input("Enter a number (<Enter> to quit) >> ")
while xStr != "":
x = eval(xStr)
num.append(x)
sum = sum + x
count = count + 1
xStr = input("Enter a number (<Enter> to quit) >> ")
num.sort()
print("You have entered", count, "number of numbers. These numbers are listed in ascending order:", num)

main()

Is This A Good Question/Topic? 0

## Replies To: Finding a number? If statement?

### #2 andrewsw

• blow up my boots

Reputation: 6523
• Posts: 26,397
• Joined: 12-December 12

## Re: Finding a number? If statement?

Posted 24 October 2014 - 01:46 PM

Yes, an if-statement. tutorialspoint

Be aware that input() returns a string "7" rather than a number 7. int() can be used to convert to an integer.

BTW What book or tutorial are you reading?

This post has been edited by andrewsw: 24 October 2014 - 01:48 PM

### #3 puck2020

Reputation: 2
• Posts: 24
• Joined: 30-August 14

## Re: Finding a number? If statement?

Posted 24 October 2014 - 02:34 PM

Python Programming: An Introduction to Computer Science Second Edition John Zelle

### #4 andrewsw

• blow up my boots

Reputation: 6523
• Posts: 26,397
• Joined: 12-December 12

## Re: Finding a number? If statement?

Posted 24 October 2014 - 02:36 PM

Okay, so that should also have a section - probably a chapter - on the if-statement.

### #5 puck2020

Reputation: 2
• Posts: 24
• Joined: 30-August 14

## Re: Finding a number? If statement?

Posted 24 October 2014 - 03:05 PM

Done and done. Thanks for the help.

def main():

sum = 0.0
count = 0
num = []
xStr = input("Enter a number (<Enter> to quit) >> ")
while xStr != "":
x = eval(xStr)
num.append(x)
sum = sum + x
count = count + 1
xStr = input("Enter a number (<Enter> to quit) >> ")
num.sort()
print("You have entered", count, "number of numbers. These numbers are listed in ascending order:", num)

if 7 not in num:
print("You did not enter the number 7.")
num.append(7)
num.reverse()
print(num)

main()

### #6 andrewsw

• blow up my boots

Reputation: 6523
• Posts: 26,397
• Joined: 12-December 12

## Re: Finding a number? If statement?

Posted 24 October 2014 - 03:14 PM

Glad you sorted, but don't use eval(); if you want to convert a string to an integer just use int().

eval is dangerous

This post has been edited by andrewsw: 24 October 2014 - 03:19 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }