How to start a program Automatically from the main method [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my code
def main():
# This code reads in data.txt and loads it into an array
# Array will be used to add friends, remove and list
# when we quit, we'll overwrite original friends.txt with
# contents
print"Welcome to the program"
print "Enter the correct number"
print "Hockey fan 1, basketball fan 2, cricket fan 3"
choice = input("Select an option")
while choice!=3:
if choice==1:
addString = raw_input("Who is your favorite player??")
print "I love Kessel"
elif choice==2:
remInt = raw_input("Do you think that the Cavaliers will continue ther loosing ways?")
print "I think they can beat the Clippers"
else:
"You must choose a Number (1,2 or 3)"
print "Cricket is a great sport"
choice = input("Select an option")
inFile = open('data.txt','r')
listNumbers = []
for numbers in inFile:
listNumbers.append(numbers)
print numbers
inFile.close()
if __name__ == "__main__":
main() # will call the 'main' function only when you execute this from the command line.

Add:
if __name__ == "__main__":
main()
To your script (indented all the way to the left; not as part of the main() function).

Add:
if __name__ == "__main__":
main()
to the end of the file

You should try the following
if __name__ == "__main__":
main()
Once that is done, you should call your program as follows from the command line (assuming you are on Linux/Mac)
python <your_prog>
Also it will be helpful if you give the exact error that you are getting.
Also make sure that all the indentation is correct.

Related

Python is stopping before running a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm having trouble with this code. It will run normally until it gets to the loop (def Game) and just stops there. I did have it as an if statement but that still didn't work. Please note that this may be a bit messy.
import random
import time
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
def Game():
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
WordSelected=("No")
LettersGuessed=0
print (TutorialDone)
EnterName = input("Would you like to enter your name?").title()
def Game(): is not a loop, it is a function it does not execute until you call it.
you can call a python function in this way
Game()
if you want to call the same function again and again simply you can call the function inside a for or while loop:
while(condition):
Game()
if your are very beginner follow some tutorials
https://www.tutorialspoint.com/python/python_functions.htm

python if variable == string wont work when you enter correct value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hi i am am trying to make a program where if you enter hi it will say you rule but if you enter anything else it will say sucker
my current code is
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword == "hi"
print("i rule")
else:
print("sucker")
please help!
just add strip to remove unwanted space/newline characters
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword.strip() == "hi":
print("i rule")
else:
print("sucker")
Use this instead:
pword = input()
Or if it's Python 2.x, use raw_input() instead.
But fix your indentation first, and add a colon after if pword == "hi".
no need to import sys(except you want to)
pword = input("enter your password")
if pword == "hi":
print("i rule")
else:
print("sucker")
If you want to the correct code is:(you forgotten an : in the if statment)
import sys
print("enter your password")
pword = (sys.stdin.readline())
if pword.strip() == "hi":
print("i rule")
else:
print("sucker")

Why isn't this code executing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
There are no syntax error or compilation error found. Why is not this program executing? I am not able to catch why is this program not running if has no compilation errors. What could logically go wrong in this below code?
What can I add into this code to make it run and interact?
def main():
print "Checking platform...\r\r\r"
platform = systemdetails()
def systemdetails():
print "Hello! Welcome to the auto-commander"
print "Please enter the platform specific number to automate."
platforminput = integer(input ("1. Cisco 2. Linux/Unix 3. Juniper 4. VMware vSphere/NSX \n:"))
if platforminput ==1:
platform='cisco_ios'
elif platforminput ==2:
platform='linux'
elif platforminput ==3:
platform='juniper'
elif platforminput ==4:
platform='vmware'
else:
print "Commander has to repeat the question...\n\n"
systemdetails()
return platform
You'll need to call your main function. A minimal example that still reproduces your problem is
def main():
print "Hello World!"
To get this to work, you need to call your main
def main():
print "Hello World!"
main()
Generally you only want to call your main if you are not being imported, which can be done like so
def main():
print "Hello World!"
if __name__ == '__main__':
main()
You need to call your main() function.
Either like this:
main()
Or like this:
if __name__ == "__main__":
main()
In the second example, main() will only be called if you run the program directly rather than importing the module and running functions individually. This way, you can import the module's functions to use separately without main() running when importing.

unexpected Indent and break out of loop in game code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i am new to python as you may be able to tell with this question. I am currently building a Rock, Paper, Scissors game to later include into a bigger program i am working on in python 3.4. the problem i am having is in the code listed below.
def computerPlayer(): #randomly selects a rock paper or scissor for computer hand
c = random.randint(0, 2)
if c==0:
y=('rock')
if c==1:
y=('scissors')
if c==2:
y==('paper')
return y
in front of the bottom line return y i am getting a unexpected Indent error, i have tried correcting this over the past day now with no results, if i move it forward i get 'return' outside function, but when i move it back i get the unexpected indent, I am honestly at a complete loss here and im not sure where to go. Any help is great thanks.
the above problem is now fixed, but i know have a break outside of loop error. it is appearing at the end of my code now. any help is great thank you.
again = raw_input('do you wish to try again? (yes\no)\n :') #Ask the user if they want play again
if again == ('yes') or again == ('sure') or again == ('okay'):
print ('')
elif again == ('no') or again == ('nah') or again == ('nope') or again == ('screw you') or again == ('screw it'):
print ('FINE THEN!!! =^( \n (Enter>>>game()<<< if you change your mind)')
#breaks the loop
break
game()
Try this:
def computerPlayer():
'''
Randomly selects a rock paper or scissor for computer hand
'''
c = random.randint(0, 2)
if c == 0:
y = ('rock')
if c == 1:
y = ('scissors')
if c == 2:
y = ('paper')
return y
Indentation is important in python, it shows where your methods and control flows start and end. In your previous code, the if statements were not indented under the method and so python could not tell that it was apart of the computerPlayer() function.
According to PEP8 ( a style guide for python ) proper indentation is 4 spaces. For more information on PEP8 and its view on indentation, check here:
http://legacy.python.org/dev/peps/pep-0008/#indentation

Can only input one thing in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a problem where I want the user to be able to input text to call functions. It works perfectly fine except for one thing. :/ Once something has been input nothing can be done afterwards. The only way to solve it is run the program again which is not convenient. I have spent a lot of time looking for an answer and need help. I also think other amateurs like me might be wondering this too.
An example of the code:
x = raw_input('test1')
if x == 'x':
print 'test2'
The result:
test1x
test2
x
'x'
As you can see it works once then stops working. For the record I'm using Python 2. Hope this can be solved :)
You need to use a loop if you want to program to keep running.
Here is a simple example:
while True:
n = raw_input("Please enter 'hello':")
if n.strip() == 'hello':
break
The program will keep running until you type hello
You can use the following function
def call():
input = raw_input('input: ')
if input == 'yes':
print 'yes'
call()
call()
last_data = ''
while last_data != 'yes':
input = raw_input('ENTER SOMETHING: ')
#do whatever you want with input
last_data = raw_input('DO YOU WANT TO QUIT? (yes/no): ')

Categories

Resources