I'm currently learning Python 3, and I have a task to: Write a program which prints out this table. (The grader will ignore spaces at the end of the line.) I'm supposed to: Focus on creating one line of the table using a loop. Think about building up a string inside the loop and then printing the string once it has all of the characters required for the one line. Then rather than using hard coded values like 32 and 48, replace the range values with variable expressions. I'm not sure where to even start.
chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: @ A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
Building a string in a loop
Page 1 of 15 Replies - 850 Views - Last Post: 01 February 2013 - 01:39 PM
Replies To: Building a string in a loop
#2
Re: Building a string in a loop
Posted 01 February 2013 - 10:07 AM
"I don't know where to start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.
#3
Re: Building a string in a loop
Posted 01 February 2013 - 10:14 AM
tlhIn`toq, on 01 February 2013 - 12:07 PM, said:
"I don't know where to start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.
I understand what you're saying, but I'm not a college student. I'm learning python independently on a tutorial site. I can email the site's assistants for help with a problem, but their replies are usually vague. So I was hoping to get a second source of advice on this site.
#4
Re: Building a string in a loop
Posted 01 February 2013 - 10:47 AM
If you are following a tutorial then move back a bit or just get a good beginners book.
This is my attempt but it's not great since strings are immutable and a new string will be created with every concatenation.
This is my attempt but it's not great since strings are immutable and a new string will be created with every concatenation.
def buildString(startIndex, endIndex):
s = ""
for i in range(startIndex, endIndex + 1):
s = s + str(i) + " "
return s
value = buildString(32, 47)
print("The value is: ", value)
#5
Re: Building a string in a loop
Posted 01 February 2013 - 11:04 AM
Think about ways to assemble strings:
You can do stuff like this:
or in a loop
or you can build a list and turn that into a string:
You can convert lists into other things with list comprehensions:
Or even simpler:
I hope some of this gives you a few ideas. The tutorials at python.org are good, as is the documentation. Spend the weekend reading over that stuff.
You can do stuff like this:
somestring = "" somestring = somestring +"foo " somestring = somestring +"bar " somestring = somestring +"baz " etc
or in a loop
substrings = ["foo", "bar", "baz"] for substring in substrings: somestring = somestring+substring
or you can build a list and turn that into a string:
somestring = ", ".join(substrings)
You can convert lists into other things with list comprehensions:
substrings = [s.capitalize() for s in substrings] somestring = ", ".join(substrings)
Or even simpler:
somestring = ", ".join([s.capitalize() for s in substrings])
I hope some of this gives you a few ideas. The tutorials at python.org are good, as is the documentation. Spend the weekend reading over that stuff.
#6
Re: Building a string in a loop
Posted 01 February 2013 - 01:39 PM
The example prints the relationship between a character and it's decimal value. Google ASCII table, and Python's ord() and chr() functions for more info.
for ctr in range(32, 48): print ctr, chr(ctr)
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|