A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Comments on my ps2b code? http://pastebin.com/rCX2nVh7 NOTE: You'll see a mix of string concatenation and string formatting with '%i' and '%s' in the printed answers at the end. I was originally going to do it all in string formatting but I kept getting an error saying that Python couldn't print the integers in the string. The syntax was solid though, so I'm guessing this had something to do with those integers being floats? I just changed it so the numbers were converted to string first and then concatenated to get past the error. Never bothered to change the first print statement too.

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    My bad! This code is actually from ps1b, not ps2b! LOL, forgot the whole counting-from-zero thing.

  2. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Hi there. Just to point out quickly; floats are not actually integers, ints are the non-floats, the whole numbers. Anyway, have you learnt how to use the string .format() method yet? You can use that for creating new strings really well. a = 50 b = 5.5 c = 'The number {0} is an example of an int, whilst {1} is a float.'.format(a, b) You can also use the str() function within concatenation expressions. cheesy_pop = 'ABC ~ It's easy like ' + str(123) I'll try and find time to have a proper look through your code soon, I'm just a bit busy at the moment.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Right! I was using 'integer' loosely there for a second without regard for it's Python meaning, my bad! And I haven't yet learnt that method. I'm going to look into that right away. Thanks man.

  4. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    No worries.

  5. carlsmith
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You will get errors if you try and concatenate a non-string into a string expression. >>>string = 'abc' + 5 # this will throw one But the print statement will convert non-strings implicitly. >>>print 'abc', 5 abc 5 Note that print will not convert part of an expression. It just converts floats and ints and stuff to strings when they are single arguments in their own right >>>print 'abc' + 5 # another error coming up >>>print 'abc', 5, 5.5 abc 5 5.5 You can do legal expressions. >>>print 'abc' + 'cde', 5 + 5 abcdef 10 I personally never use %formatting, I never like it. I just use the format method or concatenation with str() calls. Add parenthesis, backslashes, and triple-quotes, and you can avoid ever using %formatting. a = 'Bob' print 'Hello ' +str(a)+ ' how are you?'

  6. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.