SlideShare uma empresa Scribd logo
1 de 11
For any help regarding Python Homework Help
visit :- https://www.pythonhomeworkhelp.com/
Email :- support@pythonhomeworkhelp.com call
us at :- +1 678 648 4277
pythonhomeworkhelp.com
Introduction To Programming Using Python
Problem 1
Login security
One important aspect of security in computer science is the concept of hashing: taking some
text, and somehow converting it to a number. This is needed because many security
algorithms work through math, so numbers are needed.
Another important aspect is the use of the modulo operator (%). You've seen this -- it
returns the remainder portion of a division. This is useful because unlike most other math
operators, modulo is one-way. That is, I can tell you that I'm thinking of a number x, and
when I mod it by 5, I get 3, but from this information alone, you don't know whether x is 3 or
8 or 13 or 18, or ...
In this problem, we'll create a login screen, where the user must enter a password in
order to see a secret message. We will give the user 3 chances to get the password right,
and either print the secret message or a failure message (after 3 chances).
First, define a function encrypt that takes one string. It will hash the string using the built-
in Python function hash (try it on the shell) and modulo the value by a prime number (e.g.
541 -- this is very small in the computer science world but good enough for us). The
function should then return this number.
pythonhomeworkhelp.com
e.g. encrypt("my password") -> 283 (if you use 541 as the prime, for example)
At the top of the file, define a variable _KEY to be the result, e.g. _KEY = 283.
Now, write the rest of the program. Each time you ask the user for the password, call
encrypt with the user's input, and compare the value to _KEY. If the two match, the user
(most likely) entered the correct password, otherwise he loses one chance.
Problem 2 –
The game of Nims / Stones
In this game, two players sit in front of a pile of 100 stones. They take turns, each removing
between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person
who removes the last stone(s) wins.
Write a program to play this game. This may seem tricky, so break it down into parts. Like
many programs, we have to use nested loops (one loop inside another).
In the outermost loop, we want to keep playing until we are out of stones.
Inside that, we want to keep alternating players. You have the option of either writing two
blocks of code, or keeping a variable that tracks the current player. The second way is
slightly trickier since we haven't learned lists yet, but it's definitely do-able!
pythonhomeworkhelp.com
Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it
a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile
to take off this many? If any of these answers are no, we should tell the user and re-ask
them the question.
So, the basic outline of the program should be something like this:
TOTAL = 100
MAX = 5
pile = TOTAL # all stones are in the pile to start
while [pile is not empty]:
while [player 1's answer is not valid]:
[ask player 1]
[check player 1's input... is it valid?]
[same as above for player 2]
Note how the important numbers 100 and 5 are stored in a single variable at the top. This
is good practice -- it allows you to easily change the constants of a program. For example,
for testing, you may want to start with only 15 or 20 stones.
Be careful with the validity checks. Specifically, we want to keep asking player 1 for their
choice as long as their answer is not valid, BUT we want to make sure we ask them at
pythonhomeworkhelp.com
least ONCE. So, for example, we will want to keep a variable that tracks whether their
answer is valid, and set it to False initially.
When you're finished, test each other's programs by playing them
Solution
# Some constants...
LARGE_PRIME = 541
_KEY = 171 # to get this number, I used the password "solution"
MAX_FAILURES = 3 # stop when we hit this many failures
# The encrypt function. Remember, functions shouldn't be asking for input or
# printing their result. Any input a function needs (in this case, a string to
# encrypt) should be passed in, and the output should be returned.
def encrypt(text):
return hash(text) % LARGE_PRIME
# Main program code
num_failures = 0
pythonhomeworkhelp.com
# We'll keep looping until we hit the max number of failures...
# We need to break out of the loop when we get it correct also, see below.
while num_failures < MAX_FAILURES:
login = raw_input("Please enter the password: ")
if encrypt(login) == _KEY:
print "Correct!"
break # remember, this breaks out of the current loop
else:
num_failures = num_failures + 1
print "Incorrect! You have failed", num_failures, "times."
# When we get here, it's either because num_failures == MAX_FAILURES, or
# because we hit the break statement (i.e. we got the correct login), so...
if num_failures >= MAX_FAILURES:
print "Sorry, you have hit the maximum number of failures allowed."
#
# Mihir Kedia
# Homework 3 - Nim
pythonhomeworkhelp.com
#
# Notes: This program will crash if someone enters a non-integer at
the prompt.
# We haven't learned how to fix this yet.
#
print ' ____...----....'
print ' |""--..__...------._'
print ' `""--..__|.__ `.'
print ' | ` |'
print ' | | |'
print ' / | |'
print ' __' / |'
print ' ,' ""--..__ ,-' |'
print '|""--..__ ,' |'
print '| ""| |'
print '| ; , | |'
print '| ' | |"'
print '| | |.-.___'
print '| | | "---(='
print '|__ | __..''
print ' ""--..__|__..--""'
print
pythonhomeworkhelp.com
print "Welcome to Nim!"
print
print "The rules of this game are simple. You start with a pile of
stones.", 
"Each player takes turns removing between 1 and 5 stones from the pile.", 
"The player who removes the last stone wins!"
print
print
#get a valid initial pile size
pile_size = int(raw_input("How many stones would you like to start with?n"))
while pile_size <= 0:
pile_size = int(raw_input("You need to start with at least one stone in the pile!n"))
#2 players; player 1 and player 2. Start with player 1
player = 1
#main game loop
while pile_size > 0:
prompt = "Player " + str(player) + ", there are " + str(pile_size) + " stones in front of you. "
+ 
"How many stones would you like to remove (1-5)?n"
pythonhomeworkhelp.com
move = int(raw_input(prompt))
if move <= 0:
print "Hey! You have to remove at least one stone!"
elif move > pile_size:
print "There aren't even that many stones in the pile..."
elif move > 5:
print "You can't remove more than five stones."
else:
#if we're here, they gave a valid move
pile_size = pile_size - move
player = 2-(player-1) #this is kind of cute: changes 1 to 2 and 2 to 1.
#If we've exited the loop, the pile size is 0. The player whose turn it is NOW just lost the
game..
print "Player", 2-(player-1), "has won the game!"
# nims.py
# Example solution for Lab 4, problem 2.
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 3 pythonhomeworkhelp.com
TOTAL = 100
MAX = 5
pile = TOTAL # number of stones in the pile at any given time
def is_valid(x):
# returns True iff x is between 1 and MAX, and there's also enough stones,
# otherwise returns False
return (x >= 1) and (x <= MAX) and (x <= pile)
while pile > 0:
# player 1 turn
print "Player 1's turn. There are", pile, "stones in the pile."
x = 0
while not is_valid(x):
# ask
x = int(raw_input("Player 1, how many? "))
# check
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",
"you can't pick up more than there are in the pile."
pythonhomeworkhelp.com
pile = pile - x
if pile == 0:
# win -- do something
print "Congratulations, Player 1, you win!"
break
# player 2 turn
print "Player 2's turn. There are", pile, "stones in the pile."
y = 0
while not is_valid(y):
y = int(raw_input("Player 2, how many? "))
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",
"you can't pick up more than there are in the pile."
pile = pile - y
if pile == 0:
# win -- do something
print "Congratulations, Player 2, you win!"
break
print "Game over." pythonhomeworkhelp.com

Mais conteúdo relacionado

Mais procurados

The Ring programming language version 1.4 book - Part 15 of 30
The Ring programming language version 1.4 book - Part 15 of 30The Ring programming language version 1.4 book - Part 15 of 30
The Ring programming language version 1.4 book - Part 15 of 30Mahmoud Samir Fayed
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Esehara Shigeo
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our WaysKevlin Henney
 
Build a compiler in 2hrs - NCrafts Paris 2015
Build a compiler in 2hrs -  NCrafts Paris 2015Build a compiler in 2hrs -  NCrafts Paris 2015
Build a compiler in 2hrs - NCrafts Paris 2015Phillip Trelford
 
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...Patrick Niedzielski
 
Fog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notesFog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notesBrandon Weaver
 
Python basic
Python basic Python basic
Python basic sewoo lee
 
How fast ist it really? Benchmarking in practice
How fast ist it really? Benchmarking in practiceHow fast ist it really? Benchmarking in practice
How fast ist it really? Benchmarking in practiceTobias Pfeiffer
 
The Ring programming language version 1.7 book - Part 56 of 196
The Ring programming language version 1.7 book - Part 56 of 196The Ring programming language version 1.7 book - Part 56 of 196
The Ring programming language version 1.7 book - Part 56 of 196Mahmoud Samir Fayed
 
tensorflow/keras model coding tutorial 勉強会
tensorflow/keras model coding tutorial 勉強会tensorflow/keras model coding tutorial 勉強会
tensorflow/keras model coding tutorial 勉強会RyoyaKatafuchi
 

Mais procurados (13)

The Ring programming language version 1.4 book - Part 15 of 30
The Ring programming language version 1.4 book - Part 15 of 30The Ring programming language version 1.4 book - Part 15 of 30
The Ring programming language version 1.4 book - Part 15 of 30
 
Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.Fizz and buzz of computer programs in python.
Fizz and buzz of computer programs in python.
 
Ruby Intro {spection}
Ruby Intro {spection}Ruby Intro {spection}
Ruby Intro {spection}
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
 
Build a compiler in 2hrs - NCrafts Paris 2015
Build a compiler in 2hrs -  NCrafts Paris 2015Build a compiler in 2hrs -  NCrafts Paris 2015
Build a compiler in 2hrs - NCrafts Paris 2015
 
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
 
Python
PythonPython
Python
 
Fog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notesFog City Ruby - Triple Equals Black Magic with speaker notes
Fog City Ruby - Triple Equals Black Magic with speaker notes
 
Python basic
Python basic Python basic
Python basic
 
How fast ist it really? Benchmarking in practice
How fast ist it really? Benchmarking in practiceHow fast ist it really? Benchmarking in practice
How fast ist it really? Benchmarking in practice
 
The Ring programming language version 1.7 book - Part 56 of 196
The Ring programming language version 1.7 book - Part 56 of 196The Ring programming language version 1.7 book - Part 56 of 196
The Ring programming language version 1.7 book - Part 56 of 196
 
Control Flow
Control FlowControl Flow
Control Flow
 
tensorflow/keras model coding tutorial 勉強会
tensorflow/keras model coding tutorial 勉強会tensorflow/keras model coding tutorial 勉強会
tensorflow/keras model coding tutorial 勉強会
 

Semelhante a Python Homework Help

Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number TutorialOXUS 20
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - pythonSunjid Hasan
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquapariwar
 
Most asked JAVA Interview Questions & Answers.
Most asked JAVA Interview Questions & Answers.Most asked JAVA Interview Questions & Answers.
Most asked JAVA Interview Questions & Answers.Questpond
 
Class 4: For and while
Class 4: For and whileClass 4: For and while
Class 4: For and whileMarc Gouw
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptMahyuddin8
 
Learn python
Learn pythonLearn python
Learn pythonmocninja
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
MINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxMINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxNayanChandak1
 
Introduction to Genetic Algorithms with Python - Hello World!
Introduction to Genetic Algorithms with Python  - Hello World!Introduction to Genetic Algorithms with Python  - Hello World!
Introduction to Genetic Algorithms with Python - Hello World!Cℓinton Sheppard
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/elseMarc Gouw
 
1 ECE 175 Computer Programming for Engineering Applica.docx
1  ECE 175 Computer Programming for Engineering Applica.docx1  ECE 175 Computer Programming for Engineering Applica.docx
1 ECE 175 Computer Programming for Engineering Applica.docxoswald1horne84988
 

Semelhante a Python Homework Help (20)

Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
Most asked JAVA Interview Questions & Answers.
Most asked JAVA Interview Questions & Answers.Most asked JAVA Interview Questions & Answers.
Most asked JAVA Interview Questions & Answers.
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Class 4: For and while
Class 4: For and whileClass 4: For and while
Class 4: For and while
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
Learn python
Learn pythonLearn python
Learn python
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
 
Practice
PracticePractice
Practice
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
MINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxMINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptx
 
Introduction to Genetic Algorithms with Python - Hello World!
Introduction to Genetic Algorithms with Python  - Hello World!Introduction to Genetic Algorithms with Python  - Hello World!
Introduction to Genetic Algorithms with Python - Hello World!
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
 
Class 3: if/else
Class 3: if/elseClass 3: if/else
Class 3: if/else
 
1 ECE 175 Computer Programming for Engineering Applica.docx
1  ECE 175 Computer Programming for Engineering Applica.docx1  ECE 175 Computer Programming for Engineering Applica.docx
1 ECE 175 Computer Programming for Engineering Applica.docx
 

Mais de Python Homework Help

Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Homework Help
 

Mais de Python Homework Help (20)

Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Complete my Python Homework
Complete my Python HomeworkComplete my Python Homework
Complete my Python Homework
 
Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptx
 
Basic Python Programming.pptx
Basic Python Programming.pptxBasic Python Programming.pptx
Basic Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptx
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Perfect Python Homework Help
Perfect Python Homework HelpPerfect Python Homework Help
Perfect Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 

Último

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

Python Homework Help

  • 1. For any help regarding Python Homework Help visit :- https://www.pythonhomeworkhelp.com/ Email :- support@pythonhomeworkhelp.com call us at :- +1 678 648 4277 pythonhomeworkhelp.com
  • 2. Introduction To Programming Using Python Problem 1 Login security One important aspect of security in computer science is the concept of hashing: taking some text, and somehow converting it to a number. This is needed because many security algorithms work through math, so numbers are needed. Another important aspect is the use of the modulo operator (%). You've seen this -- it returns the remainder portion of a division. This is useful because unlike most other math operators, modulo is one-way. That is, I can tell you that I'm thinking of a number x, and when I mod it by 5, I get 3, but from this information alone, you don't know whether x is 3 or 8 or 13 or 18, or ... In this problem, we'll create a login screen, where the user must enter a password in order to see a secret message. We will give the user 3 chances to get the password right, and either print the secret message or a failure message (after 3 chances). First, define a function encrypt that takes one string. It will hash the string using the built- in Python function hash (try it on the shell) and modulo the value by a prime number (e.g. 541 -- this is very small in the computer science world but good enough for us). The function should then return this number. pythonhomeworkhelp.com
  • 3. e.g. encrypt("my password") -> 283 (if you use 541 as the prime, for example) At the top of the file, define a variable _KEY to be the result, e.g. _KEY = 283. Now, write the rest of the program. Each time you ask the user for the password, call encrypt with the user's input, and compare the value to _KEY. If the two match, the user (most likely) entered the correct password, otherwise he loses one chance. Problem 2 – The game of Nims / Stones In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins. Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another). In the outermost loop, we want to keep playing until we are out of stones. Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able! pythonhomeworkhelp.com
  • 4. Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question. So, the basic outline of the program should be something like this: TOTAL = 100 MAX = 5 pile = TOTAL # all stones are in the pile to start while [pile is not empty]: while [player 1's answer is not valid]: [ask player 1] [check player 1's input... is it valid?] [same as above for player 2] Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones. Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at pythonhomeworkhelp.com
  • 5. least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially. When you're finished, test each other's programs by playing them Solution # Some constants... LARGE_PRIME = 541 _KEY = 171 # to get this number, I used the password "solution" MAX_FAILURES = 3 # stop when we hit this many failures # The encrypt function. Remember, functions shouldn't be asking for input or # printing their result. Any input a function needs (in this case, a string to # encrypt) should be passed in, and the output should be returned. def encrypt(text): return hash(text) % LARGE_PRIME # Main program code num_failures = 0 pythonhomeworkhelp.com
  • 6. # We'll keep looping until we hit the max number of failures... # We need to break out of the loop when we get it correct also, see below. while num_failures < MAX_FAILURES: login = raw_input("Please enter the password: ") if encrypt(login) == _KEY: print "Correct!" break # remember, this breaks out of the current loop else: num_failures = num_failures + 1 print "Incorrect! You have failed", num_failures, "times." # When we get here, it's either because num_failures == MAX_FAILURES, or # because we hit the break statement (i.e. we got the correct login), so... if num_failures >= MAX_FAILURES: print "Sorry, you have hit the maximum number of failures allowed." # # Mihir Kedia # Homework 3 - Nim pythonhomeworkhelp.com
  • 7. # # Notes: This program will crash if someone enters a non-integer at the prompt. # We haven't learned how to fix this yet. # print ' ____...----....' print ' |""--..__...------._' print ' `""--..__|.__ `.' print ' | ` |' print ' | | |' print ' / | |' print ' __' / |' print ' ,' ""--..__ ,-' |' print '|""--..__ ,' |' print '| ""| |' print '| ; , | |' print '| ' | |"' print '| | |.-.___' print '| | | "---(=' print '|__ | __..'' print ' ""--..__|__..--""' print pythonhomeworkhelp.com
  • 8. print "Welcome to Nim!" print print "The rules of this game are simple. You start with a pile of stones.", "Each player takes turns removing between 1 and 5 stones from the pile.", "The player who removes the last stone wins!" print print #get a valid initial pile size pile_size = int(raw_input("How many stones would you like to start with?n")) while pile_size <= 0: pile_size = int(raw_input("You need to start with at least one stone in the pile!n")) #2 players; player 1 and player 2. Start with player 1 player = 1 #main game loop while pile_size > 0: prompt = "Player " + str(player) + ", there are " + str(pile_size) + " stones in front of you. " + "How many stones would you like to remove (1-5)?n" pythonhomeworkhelp.com
  • 9. move = int(raw_input(prompt)) if move <= 0: print "Hey! You have to remove at least one stone!" elif move > pile_size: print "There aren't even that many stones in the pile..." elif move > 5: print "You can't remove more than five stones." else: #if we're here, they gave a valid move pile_size = pile_size - move player = 2-(player-1) #this is kind of cute: changes 1 to 2 and 2 to 1. #If we've exited the loop, the pile size is 0. The player whose turn it is NOW just lost the game.. print "Player", 2-(player-1), "has won the game!" # nims.py # Example solution for Lab 4, problem 2. # # Aseem Kishore # # 6.189 - Intro to Python # IAP 2008 - Class 3 pythonhomeworkhelp.com
  • 10. TOTAL = 100 MAX = 5 pile = TOTAL # number of stones in the pile at any given time def is_valid(x): # returns True iff x is between 1 and MAX, and there's also enough stones, # otherwise returns False return (x >= 1) and (x <= MAX) and (x <= pile) while pile > 0: # player 1 turn print "Player 1's turn. There are", pile, "stones in the pile." x = 0 while not is_valid(x): # ask x = int(raw_input("Player 1, how many? ")) # check if not is_valid(x): print "That's not valid, it has to be between 1 and 5, and", "you can't pick up more than there are in the pile." pythonhomeworkhelp.com
  • 11. pile = pile - x if pile == 0: # win -- do something print "Congratulations, Player 1, you win!" break # player 2 turn print "Player 2's turn. There are", pile, "stones in the pile." y = 0 while not is_valid(y): y = int(raw_input("Player 2, how many? ")) if not is_valid(x): print "That's not valid, it has to be between 1 and 5, and", "you can't pick up more than there are in the pile." pile = pile - y if pile == 0: # win -- do something print "Congratulations, Player 2, you win!" break print "Game over." pythonhomeworkhelp.com