Search the Community

Showing results for tags 'python'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • General
    • General
    • Show Us Your Creations
    • Blog
    • Recommended Reading
  • One Stop Programming Tutorials
    • Python Beginner Series
  • Programming Languages
    • Python
    • C++
    • C#
    • Arduino

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start





Website URL







Found 37 results

  1. Matthew

    My Code Is Not Working

    Hey guys, I have been watching kjdelectronics on his python tutorial videos and have recently gotten to the part where you build a calculator, but I'm having trouble in the finalization process of it all. My code seems to be flawed - I keep getting a syntax error and have no idea why because I have copied the code exactly as he presents it in the video. I was wondering if anyone could look at it and tell me where my issue (bug) is located. Here is my code and a screenshot of the syntax error. #Returns the sum of num1 and num2 def add(num1, num2): return num1 + num2 #Returs the result of subtracting num1 - num2 def sub(num1, num2): return num1 - num2 #Returns the result of multiplying num1 * num2 def mul(num1, num2): return num1 * num2 #Returns the result of dividing num1 / num2 def div(num, num2): return num1 / num2 def main(): operation = input("What do you want to do? (+, -, *, /): ") if(operation != '+' and operation != '-' and operation != '*' and operation != '/'): #invalid operation print("You must enter a valid operation") else: var1 = int(input("Enter num1: ") var2 = int(input("Enter num2: ") if(operation == '+'): print(add(var1, var2)) elif(operation == '/'): print(div(var1, var2)) elif(operation == '-'): print(sub(var1, var2)) else: print(mul(var1, var2)) main()
  2. Urggg

    Rock Paper Scissors Game Problem

    There seem to be a problem, which i can't find... I have a problem, where program won;t tell corectly if i won/lost/ tied.. please help, because it's eating my mind haha
  3. Rock Paper Scissors game made simply on Python. Very very simple, great project for beginners. Also an attached .py file def main(): import random botHand = random.randint(1,3) userHand = input("Pick rock, paper or scissors ") print("You pick " + userHand) if(botHand == 1): print("Computer picks scissors") elif(botHand == 2): print("Computer picks rock") elif(botHand == 3): print("Computer picks paper") import time time.sleep(2) if ((userHand == 'rock' or userHand == 'Rock')and botHand == 1): print('You win') elif ((userHand == 'rock' or userHand == 'Rock')and botHand == 2): print('You tie.') elif ((userHand == 'rock' or userHand == 'Rock')and botHand == 3): print('You lose.') elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 1): print('You lose.') elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 2): print('You win!') elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 3): print('You tie.') elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 1): print('You tie.') elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 2): print('You lose.') elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 3): print('You win!') else: print('INVALID input, try again') import time time.sleep(1) main() while (1==1): main() import time time.sleep(1) input('Press enter to start again') EDIT: For anyone wondering, yes I do know that I do not have to import the time module every single time, and I can import it once at the start. Just wanted to clear that with you guys.
  4. Elenakretova

    Making dice game alpha

    Few programs start with all of their features, so the first version will only implement the basics. First a couple of definitions: A variable is a value that is subject to change, and they are used a lot in Python. Whenever you need your program to "remember" something, you use a variable. In fact, almost all the information that code works with is stored in variables. For example, in the math equation x + 5 = 20, the variable is x, because the letter x is a placeholder for a value. An integer is a number; it can be positive or negative. For example, 1 and -1 are both integers. So are 14, 21, and even 10,947. Variables in Python are easy to create and easy to work with. This initial version of the dice game uses two variables: player and ai. Type the following code into a new text file called import random player = random.randint(1,6) ai = random.randint(1,6) if player > ai : print("You win") # notice indentation else: print("You lose") Launch your game to make sure it works. This basic version of your dice game works pretty well. It accomplishes the basic goals of the game, but it doesn't feel much like a game. The player never knows what they rolled or what the computer rolled, and the game ends even if the player would like to play again. This is common in the first version of software (called an alpha version). Now that you are confident that you can accomplish the main part (rolling a die), it's time to add to the program.
  5. Having trouble with the last problem on my python homework.Please help me on implementing what i am missing on the code. Here is what it says: Write a Python program that shows a turtle walking back and forth between two vertical walls. Please do the following: (a) Create a 800 X 600 Turtle Graphics window (b) Draw a vertical line at x-coordinate = -300. The y-coordinates of the two end points are 200 and -200, respectively. (c) Draw another vertical line at x-coordinate = 300. The y-coordinates of the two end points are 200 and -200, respectively. (d) Create a turtle object and change the shape from arrowhead to turtle. (e) Use a loop to make the turtle walk 2000 steps. Whenever the turtle hits a wall, it turns 180 degree and continues to walk. And here is what I've got down so far: import turtle turtle.setup(800,600) turtle1=turtle.Turtle() turtle1.penup() turtle1.setposition(-300, 120) turtle1.pendown() turtle1.setposition(-300, -120) turtle1.penup() turtle1.setposition(300, 120) turtle1.pendown() turtle1.setposition(300, -120) turtle1.penup() turtle1.setposition(0,0) for turtle in turtle1: turtle1.distance(2000) turtle1.degrees(180) turtle.exitonclick()
  6. when I add a text documents in my desktop, I can not change the type to .py
  7. Good morning, this is Heike, I'm totally new to python. I could not chose an interpreter for my pycharm professional trial edition 2017.3.3. on Win7 x64. The Dropdownlist was empty in comparnes to all the videos I watchd. Whenever it comes to the point(within those videos), they have some to chose from that list. Then, I installed Anaconda3. Now I can go this way: - start pycharm - click on "File" - Click "new project" - "Pure Python" (default) - but only "Existing Interpreter" is possible. How it comes that it has only an intepreter if Anaconda is installed, and why all the videos does not mentioned it.? The other option: If I chose "New Environment" it has a "base interpreter": "C\ProgramData\Anaconda3\Python.exe" and it comes to this message finally:"Cannot set up a python SDK at Python 2.7(venv) (C:\Users\Me\venv\Script\python.exe) The SDK seems invalid" The last thing is not that important to me. I placed it here cause maybe it helps, maybe it holds also some information. Hopefully someone answers me, thanks
  8. ebrahem

    im stuck

    can anyone please help me find my problem here it keeps telling me syntax error for the last line main(): #adds def add(num1,num2): return num1 + num2 #subs def sub(num1,num2): return num1 - num2 #muls def mul(num1,num2): return num1 * num2 #devs def dev(num1,num2): return num1 / num2 #the main program def main(): chicken = input("pick what you want to do: ('+','-','*','/')") if(chicken != '+' and chicken != '-' and chicken != '*' and chicken != '/'): print("wrong input") else: banana1 = input("place your first number here:") banana2 = input("place you second number here:") if(chicken == "+"): print(add(banana1,banana2) main()
  9. dogginky

    loops and exceptions (help Noob))

    So I ve been learning Python for a couple weeks and ran across this learning series. I coded a little of my own and I need some input. It runs but I am not for sure how to clean it up. Visual Studio gives me suggestions to make it all one def, am confused. not for sure if suppose to upload file or just post the code so please inform me the correct way. Not for sure the correct way to return the input for a second guess, third guess.......etc Thanks for the great video series. Sure beats Tabor and the other guys from Microsoft. Any suggestions would be greatly welcomed. import random # Gets a random number between 1-10 secret_number = random.randint(1, 10) def main(): validInput = False while not validInput: # Gets the players guess try: user_guess = int(input("Guess a number between 1 and 10 ")) validInput = True except: print("Invalid input. Try again.") #tells player if guessed number is <>== if user_guess == secret_number: print(secret_number, "Is the correct number.") elif user_guess > secret_number: print("You guessed too high.") main() elif user_guess < secret_number: print("You guessed too low.") main() else: print("I don't understand.") main() main()
  10. hey guys, Im a newbie in python, with some experience in C++. Upon trying to install python, im still unable to access it from the command prompt. (I use a windows 8.1, DELL laptop) Anyone who can help, please reply, In case you have teamviewer, ill be more than happy to let access to my PC, to fix this problem. I have tried changing the Environment Variables as mentioned in the video, but the error persists. Regards Abhishek (An aspiring programmer)
  11. Hi! I have a problem. I cant load python. I have installed everything in my computer system thing. My python map inside my local disk C:\Python36 and that is what I have been written in the system thing. So we i load python on CMD, PowerShell or IDLE i get the same message: C:\Users\Carl Johan>python 'python' is not recognized as an internal or external command, operable program or batch file. or C:\Users\Carl Johan>py Can't find a default Python. I have tested many things. Please help:)
  12. raiprakashfca

    Facing problem with functions

    I wrote this piece of code for practice. I don't see what's wrong in it. Please help. def add(num1, num2): """This function adds the value of arguments given to it. Only works with two arguments""" return num1 + num2 def sub(num1, num2): """This function substracts the value of arguments given to it""" return num2 - num1 def mul(num1, num2): """ This function multiplies the value of arguments given to it""" return num1 * num2 def div(num1, num2): """ This function divides the value of arguments given to it""" return num2 / num1 def main(): num1 = input("What is num1?") num2 = input("What is num2?") my_result1 = add(num1, num2) my_result2 = sub(num1, num2) my_result3 = mul(num1, num2) my_result4 = div(num1, num2) print(my_result1) print(my_result2) print(my_result3) print(my_result4) main() This is the output that I am getting in debugger window - C:\Python35\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\" --multiproc --qt-support=auto --client --port 63680 --file C:/Users/Dell/PycharmProjects/ pydev debugger: process 2028 is connecting Connected to pydev debugger (build 172.3317.103) What is num1?5 What is num2?10 Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\", line 1596, in <module> globals =['file'], None, None, is_module) File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\", line 1023, in run pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2\helpers\pydev\_pydev_imps\", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/Dell/PycharmProjects/", line 25, in <module> main() File "C:/Users/Dell/PycharmProjects/", line 17, in main my_result2 = sub(num1, num2) File "C:/Users/Dell/PycharmProjects/", line 6, in sub return num2 - num1 TypeError: unsupported operand type(s) for -: 'str' and 'str' Process finished with exit code 1
  13. Apollo


    Made my first program using KJDElectronics' video tutorials of Python! This is the edited and cleaned up version i made after reading some advice about splitting up functions so that they can become more usable. Really happy with it and I'm still waiting on some more constructive criticism and ways to improve it! Here it is! """Calculator""" # Modules: import time # used as an illusion of loading for effects import random # used for random numbers in random_operation function import sys # used to exit program when needed # Operations: def add(num1, num2): # Defines addition return num1 + num2 def sub(num1, num2): # Defines subtraction return num1 - num2 def mul(num1, num2): # Defines multiplication return num1 * num2 def div(num1,num2): # Defines division try: return num1 / num2 except ZeroDivisionError: # Handles division by zero print("Division by zero cannot be done. Returning zero...") return 0 def exp(num1, num2): # Defines exponentiation return num1 ** num2 # Run operation function: def run_operation(operation, num1, num2): # Determine operation if operation == int("1"): print("Adding...") time.sleep(1) print(num1, "+", num2, "=", add(num1, num2)) elif operation == int("2"): print("Subtracting...") time.sleep(1) print(num1, "-", num2, "=", sub(num1, num2)) elif operation == int("3"): print("Multiplying...") time.sleep(1) print(num1, "*", num2, "=", mul(num1, num2)) elif operation == int("4"): print("Dividing...") time.sleep(1) print(num1, "/", num2, "=", div(num1, num2)) elif operation == int("5"): print("Raising to the power...") time.sleep(1) print(num1, "^", num2, "=", exp(num1, num2)) else: print("I don't understand") time.sleep(1) # Random operation function def random_operation(operation): for loop in range(4): # repeats random calculations 4 times valid_input = False while not valid_input: # while loop for invalid inputs try: operation = (input("What would you like me to do? add, subtract, multiply, divide, or " "raise to the power?...")) valid_input = True except ValueError: print("I don't understand...") time.sleep(1) # Defines random calculations random_num1 = random.randint(1, 100) random_num2 = random.randint(1, 100) if operation == str("add"): print("Randomizing") time.sleep(1) print(random_num1, "+", random_num2, "=", add(random_num1, random_num2)) time.sleep(1) elif operation == str("subtract"): print("Randomizing") time.sleep(1) print(random_num1, "-", random_num2, "=", sub(random_num1, random_num2)) time.sleep(1) elif operation == str("multiply"): print("Randomizing") time.sleep(1) print(random_num1, "*", random_num2, "=", mul(random_num1, random_num2)) time.sleep(1) elif operation == str("divide"): print("Randomizing") time.sleep(1) print(random_num1, "/", random_num2, "=", div(random_num1, random_num2)) time.sleep(1) elif operation == str("raise to the power"): print("Randomizing") time.sleep(1) print(random_num1, "^", random_num2, "=", exp(random_num1, random_num2)) time.sleep(1) else: print("I don't understand") time.sleep(1) print('\nPLEASURE CALCULATING WITH YOU.') time.sleep(1) print('GOODBYE...') time.sleep(1) sys.exit() # Exits program # Main calculator function: def main(): user_continue = True while user_continue: # Loops if user would like to restart program valid_input = False while not valid_input: # Loops if user enters an invalid input. Avoids error try: # Acquire user input num1 = int(input("What is number 1?...")) num2 = int(input("What is number 2?...")) operation = int(input("What would you like to do? 1. Addition, 2. Subtraction, 3. Multiplication, " "4. Division, 5. Exponentiation... Choose a number...")) valid_input = True except ValueError: # Handles any value errors print("invalid input, please try again...") time.sleep(1) run_operation(operation, num1, num2) time.sleep(1) # Ask if user wants to do random calculations or exit: restart_random = (str(input("Would you like to make more calculations? (y) for yes, anything else for no..." "\n OR would you like a random calculation?...(r) for random calculations then " "exit...anything else for no..."))) if restart_random != "y" and restart_random != "r": print("PLEASURE CALCULATING WITH YOU!") time.sleep(1) print("\nGOODBYE!") time.sleep(1) user_continue = False elif restart_random == "r": time.sleep(1) random_operation(operation) # intro-like sequence print('\n\n Calculator\n\n') time.sleep(1) print(' ---LOADING---\n') time.sleep(1) print('HI. I AM YOUR PERSONAL CALCULATOR BEEP BOOP. \n WHAT WOULD YOU LIKE ME TO CALCULATE?..\n') time.sleep(1) # call main function to start program main() # Turns my program into a module capable of being imported: if __name__ == '__calculator__': main()
  14. I was making a calculator that takes BEDMAS (Proper order of operations) into account. I'm very new (I just started today) but I managed to find a simple code for an ordinary calculator (like the ones that you find in phones, computers, etc) that works in a command line, and basing my code off of that. The problem is that I am trying to display the 'operationanswer2' of what I can assume is an integer or a variable (idk man, I'm not very good at this) but it keeps giving me a error message saying 'This inspection detects statements without any effect' He is the code. Due to fear of plagiarism I will only give you 3 parts of it that gives you how it's defined, and what I'm using to print it. operationanswer1 = (number_1 + number_2) if operation2 == '+' and operation1 == '+': operationanswer2 = (operationanswer1 + number_3) print!=operationanswer2 This is not the full code I am just afraid of plagiarism. Also I have school tomorrow so I will only be able to get back to you next evening.
  15. Lu_Laude

    Need help with code

    Hi guys. I just started learning python yesterday. I have no background in programming but I would like to learn python as my first programming language. I need help with this code: if(name == "Lu") or (age == 26): print("Hello Lu") elif(name == "lu" or age != 26): print("Welcome back") else: print("Hello") On my understanding about the or on the if statement, if I did not write "Lu" but write the number 26 it should print "Hello Lu" because from what I've come to understand that if the first variable is false it will then direct to the second variable and if that is true then it will print the code. however when i run my code and write a different name aside from Lu but write the correct number 26 it doesn't print "Hello Lu" rather it prints the "Welcome back" Can anyone help me regarding this?
  16. Johnl

    getting all records

    Hello, I’m completely new to programing with Python. I’m struggling to get all hosts beneath each other. The actual goal is to combine two string together for the required output. This is the code that I use so far: #connecting to server from pyzabbix import ZabbixAPI zapi = ZabbixAPI("") zapi.login("admin", "zabbix") print("Connected to Zabbix API Version %s" % zapi.api_version()) #hostgroups = 'Zabbix servers' #get hosts in hostgroup hosts ={"groupids": '4'}, selectInterfaces=["interfaceid"]) if hosts: host_id = hosts [0]["hostid"] print("Found host id {0}".format(host_id)) output of the above is: python.exe Connected to Zabbix API Version 2.2.2 Found host id 10084 What I would aspect is: Found host id 10084 Found host id 10085 Found host id 10086 How can I achieve this? I also attached the actual file any help would be apricated Rgds, John
  17. can someone help me build a program of incremental search method on Python (ASAP)?
  18. Wolfgang

    Python Calculator

    Hi, I basically did the same thing as in the tutorial video by my command prompt states that I have a syntax error at the else: print(multiply(num1,num2)) part. I have no idea why this is the case. Any suggestions would be greatly appreciated! Here is my code: def add(num1, num2): return num1 + num2 def subtract(num1, num2): return num1 - num2 def multiply(num1, num2): return num1 * num2 def divide(num1, num2): return num1 / num2 def main(): operation = input("What do you want to do (+,-,*,/: ") if(operation != '+' and operation != '-' and operation != '*' and operation != '/'): #invalid operation print("You must enter a valid operation") else: num1 = int(input("Enter num1: ")) num2 = int(input("Enter num2: ")) if(operation == '+'): print(add(num1,num2)) elif(operation == '/'): print(divide(num1,num2)) elif(operation == '-'): print(subtract((num1,num2) else: print(multiply((num1,num2)) main()
  19. Hi fam, I want to reed al the post in a couple facebook groups. And have a easy way to delete and categorise some post. And I want to be a able to search the post by search words. And maybe by amount comments of likes. I have no experience in programming what so ever. But I read some post and I think the best way is to use python to speak to the api and write it away with mystic. Any tips .....? Where to start .. o yeah I got Macbook Pro OX. Merci
  20. Devy

    Notepad++ Cmd issue

    today is my first day using python. i'm following the youtube channel im on lesson 2, i completed lesson 1 without issue but when i edited the .py fille with notepad++ and ran it it gave me the " "python" not recognisable error". i've correctly changed my directory to the desktop where the .py file is located. I'm not sure what to do now, any help?. i have the path as C:\Python36 and name as Python in the environmental variables
  21. Thea

    Complementary DNA sequence

    Hello! I am rather new to programming and I wanted to make a program that asks for a DNA sequence, and then returns the complementary sequence... However, I am having some difficulties. I tried a few things but now I am just stuck and hoping somebody has the time to help me... What I have so far is: def complementary_bases(sequence): complementary_bases = {'A':'T', 'T':'A', 'C':'G', 'G':'C'} return complementary_bases[sequence] sequence = input('Sequence: ') print(complementary_bases(sequence)) Any idea how I can make this work? Thanks!
  22. RandomRosa

    The starter calculator is not working

    I started watching the video on how to use python. I tried it myself without exactly copying his work, in order to understand it myself. I can't find what I'm doing wrong with the starter calculator program that I'm trying to understand, but I don't get what I'm doing wrong. Some help would be great! This is the error that I get together with the code
  23. RandomRosa

    The starter calculator is not working

    I started watching the video on how to use python. I tried it myself without exactly copying his work, in order to understand it myself. I can't find what I'm doing wrong with the starter calculator program that I'm trying to understand, but I don't get what I'm doing wrong. Some help would be great! This is the error that I get together with the code
  24. pyNoob

    Calculator debug

    I've run into an issue with the calculation function (python 3.3) where addition works but sub, mul and div each return: Traceback (most recent call last): File "./", line 22, in <module> main() File "./", line 15, in main print(subtract(var1, var2)) NameError: global name 'sub' is not defined I have also tried the entire word 'subtract' but no diff. Here is the relevant code snippet: else: var1 = int(input("Enter first value:")) var2 = int(input("Enter second value:")) if(operation == '+'): print(add(var1, var2)) elif(operation == '-'): print(sub(var1, var2)) elif(operation == '*'): print(mul(var1, var2)) else: print(div(var1, var2))
  25. shredderc4

    If Then Statements

    Hey guys, i need some help if anyone is readily available. I am working on a program for a python class in school. I'm trying to write a basic program of shipping weights and how much the cost of each pound is. Now to the direct questions. One, am i able to put multiple if then statements into one line? Such as >6 and <10. If so, how would i do that? I will copy and paste my initial program to see if you guys can give me some help. Print("CSC 122 Program #3") Print("By Cody Garner") Print("Due Sunday Oct. 11th") W = input(" Enter the weight of the package: ") if (w == <=2): print("1.50") if (w == >=2 and <=6"): print("3.00") if (w == >=6 and <=10"): print("4.00") if (w == >=10") print("4.75") Please help ASAP. Thanks guys.