Repeating a function with another function - python

for an assignment we needed to make a function that flipped a coin and another to flip it 100 times. I was able to make a function that flipped a coin, but got stuck when trying to call it a 100 times with another function. This is what I have right now:
import random
def TC():
face = random.randint(0,1)
if face == 1:
return "head"
else:
return "tail"
print TC()
def ply(flips):
for i in range(flips):
return TC()
print ply(100)
When I run it it just says 'none.' Please tell me where I am going wrong. Thank You!

Just to start, your method naming is very bad. I doubt this is how your professor is teaching you to name methods and variables. It's ugly, against Python standards and hard to read I suggest you take some time and read PEP 8 it's how python was intended to be written.
So instead of TC you should use something like flip_coin and instead of ply use something like play_coin_flip or even simply play.
Next I don't know if I'm stepping outside of what you have learned but instead of using randon.randint you can use randon.choice.
And finally, as others have said, when you return you quit any other execution in a function and return whatever variable you retrun in that statement thus nullifying any other iterations of the loop you're performing. I suggest something like the below as a better program with corrections applied to make it function as intended.
from random import choice
faces = ['head', 'tail']
def flip_coin():
face = choice(faces)
return face
def play_coin_flip(flips = 1):
for i in range(flips):
print(flip_coin)
if __name__ == "__main__":
play_coin_flip(100)

Related

I am trying to run a random variable multiple times

I am very new to python, and am running into an issue I don't fully understand. I am trying to get a random variable to run multiple times, but for some reason it just returns the same random value x times.
I am not entirely certain what to try aside from the code I have already done.
lowTreasureList = "50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon"
def ranLowLoot(lowLootGiven):
# This function returns a random string from the passed list of strings.
lootIndex = random.randint(0, len(lowLootGiven) - 1)
return lowLootGiven[lootIndex]
lowLoot = ranLowLoot(lowTreasureList)
treasureSelection = int(input())
if treasureSelection == 1:
numLowTreasure = int(input('How many treasures? '))
for i in range(numLowTreasure):
ranLowLoot(lowTreasureList)
print(lowLoot)
When I do this I get the same random treasure (numLowTreasure) times, but I am trying to get it to select a new random treasure each time.
If you haven't already, it will help to read the documentation on the random module.
There are three alternatives to random.randint that are more suited to your purpose:
random.randrange(start, stop, [step]): step is optional and defaults to one. This will save you the len(...) - 1 you are using to get lootIndex, since stop is an exclusive bound.
random.randrange(stop): uses a default start of zero and default step of 1, which will save you passing 0 as your start index.
random.choice(seq): you can pass your function's parameter lowLootGiven to this as seq, which will save you from using indices and writing your own function entirely.
As for why you're getting the repeated treasure, that's because you aren't updating your variable lowLoot in your for loop. You should write:
for i in range(numLowTreasure):
lowLoot = ranLowLoot(lowTreasureList)
print(lowLoot)
Last thing I want to say is that python is nice for writing simple things quickly. Even if there was some bigger context that you were writing this code in, I might have written it like this:
lowTreasureList = ("50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon")
if int(input()) == 1:
for i in range(int(input('How many treasures? '))):
print(random.choice(lowTreasureList))
Using the round parentheses around the tuple declaration like I did isn't necessary in this case, but I like to use it because if you want to make the tuple declaration span multiple lines, it won't work without them.
Reading documentation on standard libraries is something I almost always find helpful. I think Python's documentation is great, and if it's bit too much to digest early on, I found tutorialspoint to be a good place to start.
The problem is that in the main loop you are discarding the result of the call to ranLowLoot(). As a minimal fix, in the main loop assign the result of that function call. Use:
lowLoot = ranLowLoot(lowTreasureList)
rather than simply:
ranLowLoot(lowTreasureList)
As a better fix, ditch your function completely and just use random.choice() (which does what you are trying to do, with much less fuss):
import random
lowTreasureList = ["50 gold", "Healing Potion", "10x Magic Arrows", "+1 Magic Weapon"]
treasureSelection = int(input())
if treasureSelection == 1:
numLowTreasure = int(input('How many treasures? '))
for i in range(numLowTreasure):
lowLoot = random.choice(lowTreasureList)
print(lowLoot)

How do I execute functions via sys.argv

I'm very new to coding (started 2 days ago) and for practice a friend gave me the task of writing a program that provides either the nth Fibonacci number or the sequence up to the nth point. I successfully completed that task using input() and directly asking the user for an n, now he extended the task and asked me to try getting the same results using sys.argv
After extensive use of google I figured out how to print all the given arguments and count them, but I cannot figure out any way of using those arguments in a function. Unfortunately I also can't seem to find the right keywords for google, leaving me a little stuck in no mans land.
Here's my most recent attempt:
import sys
from math import sqrt
print('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))
Fibonacci = sys.argv[0]
value = sys.argv[1]
sequence = sys.argv[2]
def fib(value): int(1/sqrt(5)*(((1+sqrt(5))/2)**value-(((1-sqrt(5))/2)**value)))
print("The {}. value is".format(value), fib(value))
input("Close")
(Small detail, albeit unimportant: I translated the strings from German to English, which is also why it says "{}. value" rather than "{}st/nd/rd/th", the differentiation between those cases is a problem for a later date.)
Now I expect to be miles off target here, but using some of the expressions that worked for my input() based code is pretty much the last idea I have right now. Can anyone give me a pointer on how I can proceed here? Even a hint on what I should google would help, but as of now I'm completely out of ideas.
EDIT: I don't know if this is what you're supposed to do, but I've solved my problem and I thought I might as well post my code in case someone else stumbles upon this thread with a similar question. Here's my solution.
import sys
from math import sqrt
Fibonacci = sys.argv[0]
Entscheidung = (sys.argv[1])
n = int(sys.argv[2])
sequence = []
if Entscheidung == "Stelle":
def fib(n): return int(1/sqrt(5)*(((1+sqrt(5))/2)**n-((1-sqrt(5))/2)**n))
print("Die {}. Stelle beträgt:{}".format(n, fib(n)))
elif Entscheidung == "Folge":
def fib(n): return int(1/sqrt(5)*(((1+sqrt(5))/2)**n-((1-sqrt(5))/2)**n))
for i in range(n):
sequence.append(fib(i + 1))
print('[%s]' % ', '.join(map(str, sequence)))
input("Schließen")
Note that I'm still an absolute beginner and this solution might be inefficient, badly written, confusingly formatted, I wouldn't know. All I can guarantee is that it does the job.
Yup, n = int(sys.argv[2]) was the charm.
Now that you're an expert at cracking sys.argv, you might want to $ pip install click and let that package do some of the parsing for you: https://click.palletsprojects.com/en/7.x/
Conditionally executing def is an option, I suppose, but a bit odd.
Pasting the same definition into both if branches is not helpful.
Just def it once, up top.
Here's a refactoring of your code with the following changes:
Don't define the same function twice. Instead, define two separate functions, and decide which one to call depending on the desired semantics.
Don't capitalize variables. Capitalized names are conventionally reserved for class names in Python.
Remove the final input. Surely your friend wanted you to make a program which can be properly reused; requiring user interaction ruins that.
Don't use sys.argv[0] for anything. If it's not used, there's no need to capture it (and if you should need it later, it's still there).
Wrap the entry point in if __name__... so that this piece of code can be imported into another program without side effects.
from math import sqrt
def fib_nth(n):
return int(1/sqrt(5)*(((1+sqrt(5))/2)**n-((1-sqrt(5))/2)**n))
def fib_seq(n):
sequence = []
for i in range(n):
sequence.append(fib_nth(i + 1))
return sequence
if __name__ == '__main__':
import sys
entscheidung = sys.argv[1]
n = int(sys.argv[2])
if entscheidung == "Stelle":
print("Die {}. Stelle beträgt:{}".format(n, fib_nth(n)))
elif entscheidung == "Folge":
print('[%s]' % ', '.join(map(str, fib_seq(n))))

How do I write this function in Python?

*I'm a Beginner...
My friend tried to help me a bit with this but I can't seem to solve it. I'm not really sure what to do so any help would be greatly appreciated.I get the following error in averageMPG,"name stats city is parameter and global".I also wasn't really sure how to write both functions in my readData function as you can see. The problem is in the picture. I haven't succeeded in part b so I haven't moved on,
def readData(carmodelData_city):
global stats_city,stats_hwy
infile=open("carModelData_city", 'r')
stats_city=[]
for s in infile.read.split():
stats.append(float(s))
return stats_city
def read_Data(carmodelData_hwy):
global stats_city,stats_hwy
infile=open("carModelData_hwy", 'r')
stats_hwy=[]
for s in infile.read.split():
stats.append(float(s))
return stats_hwy
def averageMPG(stats_city, stats_hwy):
global stats_city,stats_hwy
totals=sum(stats_city)
length=len(stats_city)
avg1=totals/length
print("The averge mpg city is", avg1)
totals1=sum(stats_hwy)
length1=len(stats_hwy)
avg2=totals/length
print("The average mpg highway is", avg2)
average=(avg1+avg2)/2
print("The combined averge mpg is", average)
def main():
global stats_city,stats_hwy
stats_city=readData("carModelData_city", "r")
stats_hwy=read_Data("carModelData_hwy", "r")
[enter image description here][1]main()
You named a function parameter stats_city, and also declared it a global value. Those two things are incompatible.
AFAICT, none of your code actually requires anything to be global in the first place, so stop declaring everything global, and you should be fine.
Well, fine on that specific error anyway. The massive overuse of global here feels an awful lot like cargo cult programming, and you have many other problems (e.g. infile.read.split() is going to try to split the read method of the file; you forgot parens, so it's not actually calling read to get data back). You're also returning at the end of the first iteration of each loop, when I suspect you want to finish the loops and return the accumulated values. You need to learn a lot more of the basics here; please talk to a professor or a tutor.
Also, your "stats.append()" calls should probably be "stats_city.append" in the first function and "stats_hwy.append" in the second. You will return after one iteration in each function unless you adjust your idents on the return call.

Python Global Variable Not Working

I have an extremely long program for a Canasta game that I am writing, in it there is a variable that ends the turn of the player if it equals a certain value. However, the turn repeats itself.
I have read many different articles and posts on forums like this one, but none of these seem to work. I even have a debug system to print the value of the variable before the if statement, and the variable is the right value, but the if statement doesn't see the same value.
I can't give the entire code, as it is extremely long, but I can give the sections that use this variable. I am using Python 2.7.2 and cannot make files separate from the main file to import.
The first line of the program is:
endTurn=1
The following function is called when conditions to win the game are met, but I have not been able to test that this works because of the bug.
def winGame(player):
global endTurn
gameWinner=player
gameWinner["score"]+=100
endTurn=0
The following function is called when a move is made. The variable decide is a raw_input() variable.
def move(player,decide):
global endTurn
theMove=decide.lower()
#if player says to end turn
if theMove=="end":
#until player discards something
discarded=0
while not discarded:
displayHand(player)
#ask player for discard
discard=int(raw_input(" Enter the list number of the card you wish to discard:"))-1
#if discard ID is in player's hand length
if not discard<0 and not discard>len(player["hand"])-1:
#add card to discard pile and remove from hand
discardPile.append(player["hand"][discard])
del(player["hand"][discard])
discarded=1
debug("Before changing, endTurn is %s"%str(endTurn))
endTurn = 0
debug("After changing, endTurn is %s"%str(endTurn))
if theMove=="new book":
newBook(player)
if theMove=="add to book":
addBook(player)
Here is where the turn should be ended. turn(thePlayer) repeats the function. The debug statement shows the correct value, 0, but the if still reads a 1. The function this is in also has the global endTurn at the top.
debug("If ending turn, endTurn of %s should be 0."%str(endTurn))
if endTurn==1:
turn(thePlayer)
Any help is greatly appreciated!
EDIT: The code is available at http://labs.codecademy.com/CV9z#:workspace. I ask that anyone viewing the code does not modify it, so that other people can see the true code.
I fixed the bug. I removed the endTurn variable and instead made the function that ends the turn just do the turn of the next player. It also simplified my code a bit.
I am wondering though, is using this method of running a function inside of itself over and over and over again (without leaving) rather messy or maybe slowing down my program?
EDIT: I now realize that this answer does not exactly help with anyone else who is stuck with global variables...
The previous answer I posted was just wrong, as it was based on a misinterpretation of the namespace docs as I had read them (Thanks to TheifMaster for pointing out my error), so this is a SEVERELY EDITED ANSWER:
The problem is in the while loop that I was able to read when the link to the entire code was posted after the erroneous answer I gave earlier.
while gameWinner==0:
endTurn=1
turn(player1)
if not gameWinner==0:
endTurn=1
turn(player2)
The game can never get to player two in this loop until gameWinner!=0.
I added the entire code to my sandbox and changed it to:
while gameWinner==0:
endTurn=1
turn(player1)
if gameWinner==0: #if player1 did not win yet
endTurn=1
turn(player2)
However, the game is still buggy... No one can win! gameWinner is not changed globally by the winGame() function until you add it to the global statement as I have shown here.
def winGame(player):
global endTurn, gameWinner
gameWinner=player
gameWinner["score"]+=100
debug("gameWinner == " + str(gameWinner))
endTurn=0

How to split python script in parts and to import the parts in a loop?

First, sorry for my stupid title :) And here is my problem.. Actually it's not a problem. Everything works, but I want to have better structure...
I have a python script with a loop "looped" each second.
In the loop there are many many IFs. Is it possible to put each IF in a separate file and then to include it in the loop? So this way every time the loop is "looped", all the IFs will be passed, too..
There are too many conditions in my script and all of them are different generally from the otheres so I want to have some kind of folder with modules - mod_wheather.py, mod_sport.py, mod_horoscope.py, etc..
Thanks in advance. I hope I wrote everything understandable..
EDIT:
Here is a structural example of what I have now:
while True:
if condition=='news':
#do something
if condition=='sport':
#so something else
time.sleep(1)
It will be good if I can have something like this:
while True:
import mod_news
import mod_sport
time.sleep(1)
And these IFs from the first example to be separated in files mod_news.py, mod_sport.py...
perhaps you wonder how to work with your own modules in general.
make one file named 'weather.py' and have it contain the appropriate if-statements like:
""" weather.py - conditions to check """
def check_all(*args, **kwargs):
""" check all conditions """
if check_temperature(kwargs['temperature']):
... your code ...
def check_temperature(temp):
-- perhaps some code including temp or whatever ...
return temp > 40
same for sport.py, horoscope.py etc
then your main script would look like:
import time, weather, sport, horoscope
kwargs = {'temperature':30}
condition = 'weather'
while True:
if condition == 'weather':
weather.check_all(**kwargs)
elif condition == 'sport':
sport.check_all()
elif condition == 'horoscope':
horoscope.check_all()
time.sleep(1)
edit: edited according to the edit in your question. Note that I suggest importing all modules only one time, at the beginning of the script, and using its functions. This is better than executing code by importing. But if you insist, you could use reload(weather), which actually performs a reload including code execution. But I cannot stress too much that using functions of external modules is a better way to go!
Put them in functions in separate files and then Import them:
"""thing1.py
A function to demonstrate
"""
def do_things(some_var):
print("Doing things with %s" % (some_var))
``
"""thing2.py
Demonstrates the same thing with a condition
"""
def do_things(some_var):
if len(some_var) < 10:
print("%s is < 10 characters long" % (some_var))
else:
print("too long")
``
"""main_program.py"""
import thing1, thing2
myvar = "cats"
thing1.do_things(myvar)
thing2.do_things(myvar)
I believe you are looking for some kind of PHP-like include() or C prepocessor #include. You would have a file such as the included.py below:
a = 2
print "ok"
and another file which has the following code:
for i in values:
import included
and you want the result to be equivalent to
for i in values:
a = 2
print "ok"
Is it what you are looking for? If so... no, it is not possible. Once Python imports a module, the code of the module is executed and following imports of the same mode only retrieve the already imported instance of the module. The code of a module is not executed everytime it is imported.
I can invent some crazy ways of doing it (let us say, file.read() + eval(), or calling reload() in an imported module.) but it would be a bad idea anyway. I bet we can think of a better solution to your real problem :)
Perhaps all you need is to call functions in your loop; and have those functions in other modules, which you import as needed.
while true:
if condition:
from module_a import f
f()
if condition2
from module_b import g
g()
Though the above is legal Python, and so answers your question, you should in practice write all the imports at the top of your file.
You could import the needed modules if they're needed, for example:
if condition:
import weather
... do something
However I'm not sure if that's what you really want.
I have a python script with a loop "looped" each second. In the loop
there are many many IFs.
Then you must optimize the repeatedly executed tests. Suppose there are 50 IFs blocks in your code and that in a turn of the for-loop, the N th condition is True: that means that the N-1 other conditions must be tested before the N th is tested and triggers the execution of the corresponding code.
It would be preferable to do so:
# to_include.py
def func_weather(*args,**kwargs):
# code
return "I'm the weather"
def func_horoscope(*args,**kwargs):
# code
return "Give me your birth'date"
def func_gastronomy(*args,**kwargs):
# code
return 'Miam crunch'
def func_sports(*args,**kwargs):
# code
return 'golf, swimming and canoeing in the resort station'
didi = {'weather':func_weather, 'horoscope':func_horoscope,
'gastronomy':func_gastronomy, 'sports':func_sports}
and the main module:
# use_to_include.py
import to_include
x = 'sports'
y = to_include.didi[x]()
# instead of
# if x =='weather' : y = func_weather()
# elif x=='horoscope' : y = func_horoscope()
# elif x=='gastronomy': y = func_gastronomy()
# elif x=='sports' : y = func_sports()
print y
result
golf, swimming and canoeing in the resort station

Categories

Resources