开发者

Learn Python the Hard Way Exercise 11

开发者 https://www.devze.com 2023-03-28 07:12 出处:网络
I\'m trying to get through exercise 11 of Learn Python the Hard Way 开发者_运维技巧and ran into some problems. Below is what I had typed into the .py file using geedit ( working on this on a PC )

I'm trying to get through exercise 11 of Learn Python the Hard Way 开发者_运维技巧and ran into some problems. Below is what I had typed into the .py file using geedit ( working on this on a PC )

print "How old are you?",
age = raw_input('27')
print "How tall are you?",
height = raw_input('5\'8"')
print "How much do you weigh?",
weight = raw_input('180lbs')
print "So, you're %r old, %r tall and %r heavy." %(age, height, weight) 

I cannot get the %r to display the raw inputs and they tend to come out as '' on the last line what am I doing wrong?


raw_input's argument is used as the text for the prompt.

You can just do:

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

If you wanted to supply default values then you could do something like:

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")

age = age if age is not '' else '27'
height = height if height is not '' else '5\'8"'
weight = weight if weight is not '' else '180lbs'

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)


I think you might be misunderstanding the behavior of raw_input. raw_input('27') means "print 27, and then wait for the user's input." The 27 is not a default value, it's just a prompt. If you hit enter without typing anything, then the value of age will be '', not '27'.


This is how I think you want it to look?

  1 print "How old are you?",
  2 
  3 age = raw_input()
  4 
  5 print "How tall are you?",
  6 
  7 height = raw_input()
  8 
  9 print "How much do you weigh?",
 10 
 11 weight = raw_input()
 12 
 13 print "So, you're %d old, %s tall and %d heavy." %(age, height, weight)


In this exercise, you are not meant to type your age, height, etc. into the parentheses in your code. Rather, leave the parentheses blank as Zed has done in his example:

age = raw_input()

Then when you run the program in terminal, YOU type your age when asked. Same with the other variables. So at the end, the program prints back the values you entered in the terminal when prompted to answer the questions.


with zed shaw's code example he specifically says: "type this code in anyway and make it exactly the same". When you use raw_input() you are asking the user to type in their info therefore you dont already add the info. When the program is executed, typed "exactly" as he has it, it will prompt the user to input a value and whatever value is imputed will be printed to the console.


print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." %(age, height, weight) 

When running this code, the raw_input takes the input. Rather than directly giving the input, this works as question and answer prototype like, ->How old are you? ->27

->So you're 27 year old!!

More over python 2 and 3 varies As the author described use Python 2 which may give you the required output in this case. Hope it helps :) !!!!


You're typing your data e.g. '27' into your text editor when the exercise is asking you to type it into terminal.

Type the text into your text editor EXACTLY as it is in the exercise leaving the brackets empty like below:

print "How old are you?",
age = raw_input()

when you run it in terminal it will show "How old are you?" then in terminal you type "27" and hit enter it will then ask you "How tall are you" type in 5'8" and hit enter

You are having a conversation with the terminal where it asks you questions and you answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消