5 Replies - 799 Views - Last Post: 24 October 2014 - 03:14 PM Rate Topic: -----

#1 puck2020   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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   User is offline

  • blow up my boots
  • member icon

Reputation: 6551
  • View blog
  • Posts: 26,559
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 puck2020   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6551
  • View blog
  • Posts: 26,559
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 puck2020   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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()




Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6551
  • View blog
  • Posts: 26,559
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1