Nybörjare i Python

248 views
Skip to first unread message

Peter Thörn

unread,
Feb 5, 2018, 7:57:41 AM2/5/18
to Stockholm Python User Group
Hej!

Har precis börjat med Python och funderar på följande uppgift...nån som kan hjälpa mig?

Mvh Peter

Listen to our story: a boy and his father, a computer programmer, are playing with wooden blocks. They are building a pyramid. Their pyramid is a bit weird, as it is actually a pyramid-shaped wall - it's flat. The pyramid is stacked according to one simple principle: each lower layer contains one block more than the layer above.

The figure illustrates the rule used by the builders.

Your task is to write a program which reads the number of blocks the builders have, and outputs the height of the pyramid that can be built using these blocks.

Note: the height is measured by the number of fully completed layers - if the builders don't have a sufficient number of blocks and cannot complete the next layer, they finish their work immediately.


Början på kod


blocks = int(input("Enter number of blocks: "))


while blocks 

else hight = 3


print("Height of the pyramid:",height)

Javier Ubillos

unread,
Feb 5, 2018, 8:31:38 AM2/5/18
to python-s...@googlegroups.com
Det låter som att någon fått en hemuppgift :)


Du kan ju räkna ut hur många block som finns i varje lager.
E.g. "lager 1" = 1
lager 2 = 2

etc...

Så, du kan subtrahera antalet använda blocks från input:en .

t.ex. input: 8
lager 1: totalt -1 
lager 2: totalt -3
lager 3: totalt -6
lager 4: totalt -10 (oj!   8 - 10 < 0, det går ju inte, rätt svar ligger i förra 'lagret')


Lyckatill med uppgiften!

// Javier

--
Det här meddelandet skickas till dig eftersom du prenumererar på gruppen "Stockholm Python User Group" i Google Grupper.
Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till python-stockholm+unsubscribe@googlegroups.com.
Om du vill göra inlägg i den här gruppen skickar du ett e-postmeddelande till python-stockholm@googlegroups.com.
Besök gruppen på https://groups.google.com/group/python-stockholm.
Fler alternativ finns på https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages