First of all, I use windows so i couldn't make curses work in windows.
I'm trying to make a login interface while an ASCII Animation is Looping.
def animation():
while True:
#something that do animation
def login_screen():
uname = str(input("Username : "))
passw = str(input("Password : "))
print("Welcome {}".format(uname))
so how do i do this in one console screen ? this is for example :
Related
I am creating a simple number only user input for CLI and also created it. But while using keyboard module when the user tries to press enter, the program closes perfectly, but the text that user enters gets executed in command line.
import keyboard
specials = "1234567890"
userinput = ""
no_of_presses = 0
print("Number to be doubled : ",end="",flush=True)
def show(e):
global no_of_presses, userinput
if(e.name in specials):
print(e.name,end="",flush=True)
no_of_presses+=1
userinput+=e.name
def output(e):
global userinput
print(f"\noutput : {int(userinput)*2}")
keyboard.on_press(show)
keyboard.on_press_key("enter",output)
keyboard.wait("enter")
Here is the image of sample error I am getting
I am completely novice in Python and in Raspberry Pi. However, I have one, maybe easy to solve problem. I want to control LED diod on RPi4 with a keyboard.
If I press "1", I want the LED be active, and if I press "0" or any other key, I want to LED be inactive. I am running Python 3.7.3 on my Raspberry Pi4. The code below is working, but when I press "1" or "0", I have to run my code via command line again if I want to change status of LED.
Is there any solution, how to still read an input from keyboard and automatically based on it change the status of the LED?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
user_input = input("Input value 0 or 1: ")
print(user_input)
while (True):
if user_input == "1":
GPIO.output(14,GPIO.HIGH)
time.sleep(1)
else:
GPIO.output(14,GPIO.LOW)
time.sleep(1)
You are only asking for the user input once. move the input(...) within the while loop.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
while True:
user_input = input("Input value 0 or 1: ")
print(user_input)
if user_input == "1":
GPIO.output(14,GPIO.HIGH)
time.sleep(1)
else:
GPIO.output(14,GPIO.LOW)
time.sleep(1)
I have tried multiple ways (crontab and rc.local) of going about this. I have triple checked my code and processes each time but to no avail.
I am making a father's day gift with a button that says his favorite word, "No!".
Every time I reboot, my program does not respond at all to the GPIO and the button attached.
Is it how I have things structured in my sloppy code?
I'm not native in Python so I beg of you to help me.
When ran from the terminal or through Thonny, it runs perfectly. But no matter what, doesn't boot at start-up!
Here is my rc.local
Here's my code for the program:
import pygame
from random import randint
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pygame.mixer.init()
#pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
proverbFile = 'proverbs/Pro1.mp3'
num = 5
def randNo ():
noFile = '/home/pi/Desktop/audio/no/No1.mp3'
print('Executed!')
default = 1
global num
pulled = 1
while num == pulled:
pulled = randint(1,4)
print('Same!')
num = pulled
print(num)
noFile = noFile.replace(str(default), str(num), 1)
print(noFile)
pygame.mixer.music.load(noFile)
pygame.mixer.music.play()
default = num
def button_callback(channel):
print("Button was pushed!")
randNo()
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback)
#while True:
# keyPress = input('Press q to quit: ')
# if keyPress == 'q':
# break;
#GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
If you want a script to run when you boot into the LXDE environment, you could take a look at this Raspberry Pi forum post:
Navigate to ~/.config/lxsession/LXDE-pi
Open the autostart file in that folder:
$ sudo nano autostart
Add #midori on a new line. If you want to run something like a python script, put something like #python mypython.py on a new line. Running a script file would be #./superscript, but for some reason the script runs in an infinite loop (perhaps this will stop that).
Save and exit: Ctrl+X, Y, Enter
Restart your Raspberry Pi into the LXDE environment.
answer from here
I haven't started on the web typing part but I need help with the starting part.
Here is my code
import webbot
import os
import time
import random
os.system('pip3 install pyautogui')
driver = webbot.Browser()
print("")
driver.go_to('https://kahoot.it/')
code = input("The game id: ")
input_elements = driver.find_elements(xpath='//input')
driver.type(driver.Key.TAB,into=input_elements[0].text)
driver.type(code)
driver.type(driver.Key.ENTER,into=input_elements[0].text)
time.sleep(1.5)
username = "test"
input_elements = driver.find_elements(xpath='//input')
driver.type(driver.Key.TAB,into=input_elements[0].text)
driver.type(username)
driver.type(driver.Key.ENTER,into=input_elements[0].text)
time.sleep(2)
driver.execute_script("window.open(' ');")
time.sleep(2)
driver.go_to('https://kahoot.it/')
# keeps the loop running
while True:
time.sleep(1)
I use repl.it's pygame for this
Edit: nvm I fixed the new tab now I need to type and hit enter into the boxes
I tried to make it only ask "do you want to continue" 3 times but it doesn't seem to work, it just kept on running. How do I fix this? It is a chat-response program which the computer askes one question and the user response.
def choice():
prompts = [feeling, homesick, miss]
random.choice(prompts)()
for item in range(3):
choice()
This is the code I have written for it. but it does not work.
import random
name = input("What is your name? ")
def restart():
restart=input('do you want to continue? ')
if restart=='yes':
choice()
else:
print("ok, see you later!")
exit()
def feeling():
response = input("How are you feeling right now {name}?".format(name=name))
if response == "tired":
tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print(random.choice(tired))
restart()
else:
print("Sorry, I don't understand what you mean by "+response+".")
exit()
def homesick():
response = input("Do you miss your home? ")
if response == "yes":
yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print(random.choice(yes))
restart()
else:
print("Sorry, I don't understand what you mean by "+response+".")
exit()
def miss():
response = input("Who do you miss?")
if response == "my mom":
print("Mom will be in town soon")
restart()
else:
print("Sorry, I don't understand what you mean by "+response+".")
exit()
def choice():
prompts = [feeling, homesick, miss]
random.choice(prompts)()
for item in range(3):
choice()
The comment from darvark is correct. If you want to keep the rest of your code the same, then I would just modify the restart function to look something like this:
import sys
def restart():
if input('do you want to continue? ') != 'yes':
sys.exit()
This way, if the user responds with anything other than 'yes', the program will quit; however, if they respond with 'yes', then the call to restart will simply do nothing, and your loop should advance to the next iteration.
One more note: It is not recommended to call the exit function within a program, since it is just a helper function to be used when you're running the Python interpreter. Within a program, you should import the sys module and call sys.exit. Source: Difference between exit() and sys.exit() in Python