11 Replies - 3652 Views - Last Post: 24 September 2012 - 10:52 PM Rate Topic: -----

#1 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

How do you put variables into strings in python?

Posted 23 September 2012 - 07:52 PM

trying to print this out, but when i debug it, it comes out error.
Welcome to Lost Fortune!

Please answer the following questions for your personalized adventure:

Enter the number of Explorers:
17
Enter the number of Explorers Lost in Battle:
13


You bravely led 17 adventurers on a quest for gold.
The group fought a band of ogres and lost 13 members.
Only 4 survived.

My code
print("Welcome to Lost Fortune!") 
print("Please answer the following questions for your personalized adventure:")

print("Enter the number of Explorers:")
x = input()
valx = float (x)

print("Enter the number of Explorers Lost in battle:")
h = input()
valh = float (h)

print("You bravely led {}.".format(valx)
      ("adventurers on a quest for gold."))
print("The group fought a band of orges and lost{} members.".format(valh) 



I did the same thing with a different code and it worked fine. Why is this one not working?

Is This A Good Question/Topic? 0
  • +

Replies To: How do you put variables into strings in python?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 08:37 PM

Line 14 is missing the end parenthesis for the print statement, which would be a syntax error. (I'm guessing that is the error you are getting, since you didn't bother showing us the error message...)

You seem to be trying to add the text on line 13 to the text on line 12, but you don't add it with a + char. You need to do that, or just include the text in the first string.

Note that when you use the format function like that, you don't actually have to do it directly after the placeholder. You can do stuff like this.
name = "John Doe"
print "Hello, {}. This is an example!".format(name)


Doing something like this, which is what you seem to be trying to do, is unnecessary.
name = "John Doe"
print "Hello, {}.".format(name) + " This is an example!"



Also, about how you are reading the input. Why are you using float()? Are you expecting to get partial explorers? "You bravely lead 5.75 explorers...". It doesn't make sense that way. - You should be expecting whole numbers; integer values. Which means you should be using int() instead of float().
Was This Post Helpful? 2
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 08:42 PM

I saw 2 little errors, first, you're missing a paren at the end of the code. Also, line 13 doesn't work alone, you gotta have it up at line 12, here's what I have
print("Welcome to Lost Fortune!") 
print("Please answer the following questions for your personalized adventure:")

print("Enter the number of Explorers:")
x = input()
valx = float (x)

print("Enter the number of Explorers Lost in battle:")
h = input()
valh = float (h)

print("You bravely led {}.".format(valx)"adventurers on a quest for gold.")
print("The group fought a band of orges and lost{} members.".format(valh))

EDIT: One more thing(I'm assuming you're using python3).
print("Enter the number of Explorers:")
x = input()
Could be rewritten as
x = input("Enter the number of Explorers:")


EDIT2:
If the average household has 2.5 kids, then why can't this guy bravely lead 5.75 explorers? lol

This post has been edited by atraub: 23 September 2012 - 08:57 PM

Was This Post Helpful? 2
  • +
  • -

#4 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 08:52 PM

Float i thought you used when you put variables.
I'm new to coding i know a little more in C++ than python
No I'm using python4.1

I was able to correct my mistakes.
print("Welcome to Lost Fortune!") 
print("Please answer the following questions for your personalized adventure:")

print("Enter the number of Explorers:")
x = input()
print("Enter the number of Explorers Lost in battle:")
h = input()


print("You bravely led {}".format(x) + "adventurers on a quest for gold.")
print("The group fought a band of orges and lost {}".format(h) + " members")

Was This Post Helpful? 0
  • +
  • -

#5 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 09:04 PM

If i were to subtract the 2 integers
Can I subtract it this way? The book I have is out of date so it is no help
print("You bravely led {}".format(x) + "adventurers on a quest for gold.")
print("The group fought a band of orges and lost {}".format(h) + " members.")
print("only {}".format(x - h) + "survived")


 File "f:\lost fortunes.py", line 14, in <module>
Syntax Error: print("The group fought a band of orges and lost %s members.".%valh: f:\lost fortunes.py, line 1463

Was This Post Helpful? 0
  • +
  • -

#6 iaremarie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-September 12

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 10:56 PM

print("only {} survived.".format(valx - valh))
print("The party was about to give up when they stumbled upon the buried")
print("fortune of 750 gold pieces. The group split the loot evenly and as")
print("the quest leader you kept the extra {} gold pieces.").format(valh//750)


Please need help
What am i doing wrong?
Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 11:34 PM

Yes you can subtract it that way.
>>> x,y = 5,2
>>> print ("{0} - {1} is {2}.".format(x,y,x-y))
5 - 2 is 3.



Error is probably this line, as told to you by Python:
print("The group fought a band of orges and lost %s members.".%valh

You don't need a dot on that one for the percent formatting. Do it like this:
>>> x = "Sheena"
>>> print "%s is a punk rocker" %x
Sheena is a punk rocker


Read this: Python Tutorial - Input and Output
Was This Post Helpful? 1
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: How do you put variables into strings in python?

Posted 23 September 2012 - 11:53 PM

Quick note: the % string formatting is depreciated, it's better to use the .format() notation which you begun with, especially for Python 3.x which you seem to be using.

This post has been edited by Simown: 23 September 2012 - 11:56 PM

Was This Post Helpful? 1
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How do you put variables into strings in python?

Posted 24 September 2012 - 12:01 AM

Hmmm, Python 3, interesting. I'm still learning Python 2, so I didn't know that. Thanks! ^^
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: How do you put variables into strings in python?

Posted 24 September 2012 - 06:26 PM

I gotta point this out...

There's no such thing as Python 4 as of right now. You're probably using Python 3.

Also, the % operator is deprecated in Python 3 and 2.7. SIDENOTE: I still haven't taken the time to really learn the format function... someone should really write a tutorial on it!

Lastly, there's no reason to come on here asking if something will work. Test it yourself and see what happens!!!

This post has been edited by atraub: 24 September 2012 - 10:58 PM

Was This Post Helpful? 3
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: How do you put variables into strings in python?

Posted 24 September 2012 - 06:51 PM

View Postatraub, on 25 September 2012 - 01:26 AM, said:

Lastly, there's no reason to come on here asking if something will work. Test it yourself and see what happens!!!


This is most definitely true, especially for Python.

By the way, if something is deprecated in Python, will that throw an error? Because I tested the % formatting and I have Python 2.7.
Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

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

Re: How do you put variables into strings in python?

Posted 24 September 2012 - 10:52 PM

Deprecated only means that they will drop support of it in a future release, but it still currently works. Python doesn't throw deprecation warnings that I know of.

EDIT:
I might have misspoke. It might not be deprecated in 2.7... that would make sense since they have already said there will NEVER be a 2.8.

This post has been edited by atraub: 25 September 2012 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1