Python Forum
Quick Help - Timers - Need Small Solution for Working Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick Help - Timers - Need Small Solution for Working Code
#1
Good Evening,

I am looking to get some help as programming is not my game, I am into networking.

To explain intentions:

This code is being used locally at home on a device that is not connected to the internet. This code is being used for a PlayStation 4 Competitive Basketball team to automate play calling. I will attach what I have at the present time, it is working code, but needs to be regenerated/re-executed each time I want to see a new play. I would like the script to run on like a 30 to 60 second timer until I stop the program.

import random

def random_line(pname):
	lines = open(pname).read().splitlines()
	return random.choice(lines)
print(random_line('listofplays.txt'))
Output:
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> RESTART: C:\Users\HOUSTONE\Desktop\NBA2K Play Calling Script\playcallscript.py Pick & Roll (1/3) (HARD) >>> RESTART: C:\Users\HOUSTONE\Desktop\NBA2K Play Calling Script\playcallscript.py Pick & Pop (1/4) (SOFT) >>> RESTART: C:\Users\HOUSTONE\Desktop\NBA2K Play Calling Script\playcallscript.py Pick & Pop (1/4) (HARD) >>>
Any help is greatly appreciated!!!
Reply
#2
Here you go. This should do the trick
import random
import time
 
def random_line(pname):
    lines = open(pname).read().splitlines()
    return random.choice(lines)

def start():   #if you dont want to have to type start for it to work just delete this and backspace everything back one
    for x in range(1,10):  #change the 10 to how many times you want it to print
        print('')
        print(random_line('listofplays.txt'))
        print('')
        Go = input('Hit Enter to See the Next Play') 
Reply
#3
(Jan-19-2019, 11:47 PM)SheeppOSU Wrote: I would like the script to run on like a 30 to 60 second timer until I stop the program.
schedule works fine for this.
Example call function every minute.
import random, time
# pip install schedule
import schedule

def random_line(pname):
    lines = open(pname).read().splitlines()
    print(random.choice(lines))

if __name__ == '__main__':
    schedule.every(1).minutes.do(random_line, 'listofplays.txt')
    while True:
        schedule.run_pending()
        time.sleep(1)
Reply
#4
(Jan-19-2019, 11:47 PM)SheeppOSU Wrote: Here you go. This should do the trick
import random
import time
 
def random_line(pname):
    lines = open(pname).read().splitlines()
    return random.choice(lines)

def start():   #if you dont want to have to type start for it to work just delete this and backspace everything back one
    for x in range(1,10):  #change the 10 to how many times you want it to print
        print('')
        print(random_line('listofplays.txt'))
        print('')
        Go = input('Hit Enter to See the Next Play') 

Sorry I didn't get back, thank you all for your input, I added +1 for you SheeppOSU as well as snippsat. I've since expanded on this code a tad. I currently have a IF-STATEMENT that isn't functioning properly. I will post the modified code and output below.

The issue with my script is very small. It lies within my IF STATEMENT. If you take a look at the code you will see what the IF STATEMENT is setup to do, however it does not do this. It will print the second option, or the ELSE portion every time. Outside the if-statement, all the code works as long as you have a list associated with the script to point to.

import random
import time

def random_line(pname):
    lines = open(pname).read().splitlines()
    return random.choice(lines)

def real_world_fn():
     for x in range(1,10):  #change the 10 to how many times you want it to print
        print('')
        print(random_line('listofplays.txt'))
        print('')
        Go = input('Hit Enter to See the Next Play')
    
def nba2k_fn():
    for x in range(1,10):  #change the 10 to how many times you want it to print
        print('')
        print(random_line('listofplays.txt'))
        print('')
        Go = input('Hit Enter to See the Next Play')

print('Welcome to the eSports Play Call App')
print('')
print('')
print('1. Real World Scenario')
print('2. NBA2K')
print('')
print('')
#print('The eSports Play Call App functions on two levels, please choose from the following')
Menu = input('The eSports Play Call App functions on two levels, please choose operational level: ')
if Menu == 1:
    print('HELLO REAL WORLD')
else:
    print('HELLO NBA2K')
Output:
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> = RESTART: C:\Users\HOUSTONE\Desktop\ITN290-6C1 - IT Capstone Project\drafts\v1\new 4.py Welcome to the eSports Play Call App 1. Real World Scenario 2. NBA2K The eSports Play Call App functions on two levels, please choose operational level: 1 HELLO NBA2K >>> = RESTART: C:\Users\HOUSTONE\Desktop\ITN290-6C1 - IT Capstone Project\drafts\v1\new 4.py Welcome to the eSports Play Call App 1. Real World Scenario 2. NBA2K The eSports Play Call App functions on two levels, please choose operational level: 2 HELLO NBA2K >>>
Let me know what yall think the issue is, THANKS!!!
Reply
#5
input returns a string. Your if statement compares that string to a number. Put quotes around the number or use int() to convert your input string to an integer.
Reply
#6
(Nov-16-2020, 06:36 PM)jefsummers Wrote: input returns a string. Your if statement compares that string to a number. Put quotes around the number or use int() to convert your input string to an integer.

Hey thanks for the reply. I had thought for a good while earlier that this could be the issue, STRINGS vs. INTEGERS that is. I actually had tried swapping out the '==' for '=' in my script but that didn't work. This did, thanks!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 909 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 836 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 977 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,031 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  small code for sampel asn1ate borys 0 841 Jul-26-2022, 10:48 AM
Last Post: borys
  Can someone explain this small snippet of code like I am a 5 year old? PythonNPC 3 1,201 Apr-08-2022, 05:54 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,866 Mar-21-2022, 10:12 AM
Last Post: End3r
  please looking for typo in my code (solution please) jamie_01 1 1,243 Jan-12-2022, 06:45 AM
Last Post: Gribouillis
  i need help with a small code Jacobthefirst 1 1,461 Sep-22-2021, 03:33 PM
Last Post: bowlofred
  Newbie - code solution explained Stjude1982 2 1,790 Sep-16-2021, 08:54 AM
Last Post: Stjude1982

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020