What is this? Not a string.. - Page 4 - Overclock.net - An Overclocking Community

Forum Jump: 

What is this? Not a string..

Reply
 
Thread Tools
post #31 of 44 (permalink) Old 11-12-2018, 03:22 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
Thank you for being annoyingly clear lol, that's my favorite kind of clear. Really, I think it helps when it comes to communicating via text. Since you can't get body language, tone or inflection of their voice. Up until this point I didn't know things could just pop into existence like that. I thought the for loop would need something to reference, and animal would have to be laid out somewhere else for it to be able to find.

Also thank you for the explanation about cheeseburger. But I'd still ask where anti_clockwise is getting all those numbers for cheeseburger, in post #28. Then again he said java, so I don't know if that is just a java thing.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #32 of 44 (permalink) Old 11-14-2018, 05:20 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
And now this! What exactly did the sort function do here? Maybe I'm blind, but I don't see that anything has actually been accomplished by it.

Code:
inventory = {
  'gold' : 500,
  'pouch' : ['flint', 'twine', 'gemstone'], 
  'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'],
  'pocket' : ['seashell','strange berry','lint']
}


inventory['backpack'].remove('dagger')

inventory['backpack'].sort()
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort()

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #33 of 44 (permalink) Old 11-30-2018, 09:09 PM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
Here it is again. Things popping into existence after for. The instructions were as follows:

"Write a statement in the indented part of the for-loop that prints a number equal to 2 * number for every list item."


Code:
my_list = [1,9,3,8,5,7]

for number in my_list:
  # Your code here
  print 2 * number
I'm still hung up on that. How could it know what "number" is? Is it because there is only 1 thing in "my_list"? And thats all it possibly could be? Python just checks what is inside the list and says "Well he must be talking about these numbers because there's only integers in there". And then just automatically assigns the items contained in the list to the "number" variable. Is that how this works?

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Sponsored Links
Advertisement
 
post #34 of 44 (permalink) Old 12-01-2018, 01:07 AM
New to Overclock.net
 
deepor's Avatar
 
Join Date: Feb 2013
Posts: 4,736
Rep: 474 (Unique: 320)
Yeah, that's how it works. The 'for' loop takes elements out of a list one by one, and it creates a variable with the name you mention so that you can use that name inside the 'for' loop's body to address your list element.

The name used here is "number", that's the variable that the 'for' loop will create.

The body of the 'for' loop here is that "print 2 * number" line. It will get run repeatedly, one time for each element in your list. The "number" variable will contain the element from the list.

That "number" variable is just a name that the person who wrote this has chosen. It doesn't mean that the list elements have to be integers. The list elements could be anything.
deepor is online now  
post #35 of 44 (permalink) Old 12-04-2018, 12:06 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
Well I'll just continue to pick your brain forever I guess. Why is it necessary to basically say the same thing twice in order to add 50 gold to the inventory? So in order to add 50 gold, I have to say:

Code:
inventory['gold'] = inventory['gold'] + 50
It seems to me that: inventory['gold'] + 50 would be sufficient.


here is the whole thing in case its needed.

Code:
inventory = {
  'gold' : 500,
  'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
  'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 

# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] = inventory['gold'] + 50

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲

Last edited by PhotonFanatic; 12-09-2018 at 02:23 AM.
PhotonFanatic is offline  
post #36 of 44 (permalink) Old 12-04-2018, 03:01 AM
New to Overclock.net
 
deepor's Avatar
 
Join Date: Feb 2013
Posts: 4,736
Rep: 474 (Unique: 320)
There's a "+=" shortcut that you can use:

Code:
inventory['gold'] += 50
About why you need to write "x = x + 50" instead of just "x + 50", I don't know how to explain best. Perhaps what's confusing you is that those "=" and "+" symbols are not really the ones you know from equations in math. In math you are describing how things relate with those symbols, but in (most) programming languages those symbols are not descriptions of how things relate, they are instead commands for the machine.

"a + b" <-- read two values and return the result of adding them

"left = right" <-- read the value on the right side and write it into the target on the left side
deepor is online now  
post #37 of 44 (permalink) Old 12-04-2018, 04:17 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
Yep makes sense now lol. That is one that I think I already knew, but it looks confusing for some reason. Kind of like telling a variable to do something new with itself. Like updating a variable or something.

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #38 of 44 (permalink) Old 12-06-2018, 12:58 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
I recently restarted that lesson to help memorize the rules. As I frequently do. And on the very first exorcise, I see something I've never seen before. Didn't notice it the first time. Why is there a semicolon after the list? Its outside the end bracket on the list. What purpose does this serve? Seems like it wouldn't really be needed. You could've started the if statement without that, couldn't you? And all would've been well.


Code:
zoo_animals = ["pangolin", "cassowary", "sloth", "bat"];
# One animal is missing!

if len(zoo_animals) > 3:
  print "The first animal at the zoo is the " + zoo_animals[0]
  print "The second animal at the zoo is the " + zoo_animals[1]
  print "The third animal at the zoo is the " + zoo_animals[2]
  print "The fourth animal at the zoo is the " + zoo_animals[3]

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
post #39 of 44 (permalink) Old 12-06-2018, 04:13 AM
New to Overclock.net
 
deepor's Avatar
 
Join Date: Feb 2013
Posts: 4,736
Rep: 474 (Unique: 320)
The person just made a tiny mistake. There's other languages where you put a semicolon after every statement, so this could have just been a sort of muscle memory for the person who typed this.

If you've never seen it, in Python you can use a semicolon to write stuff on a single line instead of with line-breaks:

Code:
>>> print "hello"; print "hi"
hello
hi
That's why the ";" wasn't an actual mistake. Python won't complain about it being there.
deepor is online now  
post #40 of 44 (permalink) Old 12-08-2018, 02:34 AM - Thread Starter
New to Overclock.net
 
PhotonFanatic's Avatar
 
Join Date: Dec 2009
Posts: 1,878
Rep: 19 (Unique: 18)
Oh nice. I always thought it would throw an error if the slightest thing were out of place.

But now I'd like to ask you to take a look at this code. The exorcise appears broken on their site. It either never tells you if you got it wrong, (just does nothing) or marks it wrong with no error message. And it always gives you the python error, along with some words from them with a little help about where you went wrong.

The instructions are: Write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list. Then sort square_list!


So I wrote:

Code:
start_list = [5, 3, 1, 2, 4]
square_list = []

for the_numbers in start_list:
  start_list.append(the_numbers ** 2)
  square_list.sort()
  
print square_list

"Executing execution.exe"
Bang for the buck
(12 items)
CPU
3570k @ 4.6Ghz @1.35v
Motherboard
Gigabyte Z77x UD3H
GPU
evga GTX 970 SSC
RAM
16Gb Gskill Ripjaws 2133Mhz @ 9-11-10-28
Hard Drive
Samsung Evo 850
Power Supply
Silverstone 900 watt
Cooling
Thermalright Ultra 120 Extreme
Case
Silverstone Raven (1st edition)
Operating System
Windows 10 Spy Delux
Monitor
Xstar 27" PLS 1440p
Keyboard
Wireless ergonomic
Mouse
Logisys Wireless trackball
▲ hide details ▲
PhotonFanatic is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off