4 Replies - 635 Views - Last Post: 10 February 2011 - 08:40 AM Rate Topic: -----

#1 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Input a String as a Value

Posted 09 February 2011 - 02:46 PM

I am just starting out learning python and i have a book that im using to help me out. I ran one of the examples included and after inputing my name on the first line it gave me this error
Traceback (most recent call last):
  File "/home/nate/Documents/Python/py3e_download/source/chapter02/useless_trivia.py", line 6, in <module>
    name = input("Hi.  What's your name? ")
  File "<string>", line 1, in <module>
NameError: name 'Nathan' is not defined

When inputing all number and no strings for the values, i get this output
Traceback (most recent call last):
  File "/home/nate/Documents/Python/py3e_download/source/chapter02/useless_trivia.py", line 14, in <module>
    name.lower())
AttributeError: 'int' object has no attribute 'lower'


I was just wondering how I can input a string as a value?

# Useless Trivia
#
# Gets personal information from the user and then
# prints true but useless information about him or her

name = input("Hi.  What's your name? ")

age = input("How old are you? ")
age = int(age)

weight = int(input("Okay, last question.  How many pounds do you weigh? "))

print("\nIf poet ee cummings were to email you, he'd address you as",
      name.lower())
print("But if ee were mad, he'd call you", name.upper())

called = name * 5
print("\nIf a small child were trying to get your attention",)
print("your name would become:")
print(called)

seconds = age * 365 * 24 * 60 * 60
print("\nYou're over", seconds, "seconds old.")

moon_weight = weight / 6
print("\nDid you know that on the moon you would weigh only",
      moon_weight, "pounds?")

sun_weight = weight * 27.1
print("On the sun, you'd weigh", sun_weight, "(but, ah... not for long).")

input("\n\nPress the enter key to exit.")


Is This A Good Question/Topic? 0
  • +

Replies To: Input a String as a Value

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Input a String as a Value

Posted 09 February 2011 - 02:48 PM

You're using Python 2.x, so you need to use raw_input() instead of input() for strings. For ints, use int(raw_input()) for floats use float(raw_input()) etc...

This post has been edited by atraub: 09 February 2011 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Input a String as a Value

Posted 09 February 2011 - 02:59 PM

View Postatraub, on 09 February 2011 - 02:48 PM, said:

You're using Python 2.x, so you need to use raw_input() instead of input() for strings. For ints, use int(raw_input()) for floats use float(raw_input()) etc...

OK thanks. Should I upgrade to 3.x or stay w/ 2.x? I have read a lot of libraries aren't supported in 3.x
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Input a String as a Value

Posted 09 February 2011 - 03:06 PM

You should read this thread if you want to find the differences:
http://www.dreaminco...learn-27-or-31/
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: Input a String as a Value

Posted 10 February 2011 - 08:40 AM

You read my mind Dogstopper, I was going to link that thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1