Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Ned solution for specific button press
-
Hello.
I am new to programing and Python. And I have created a simple program that randomize the output (it's a magic 8 ball) the program works fine. But now to My problem:
I created a loop so long as the user dosen't press "n" the program restarts, however Im trying to find a command so that only "j" restarts the program as of now all letters except n restarts the program. Here is the code:import random import console val = "" while val != "n": console.clear() def getAn(ansnu): if ansnu == 1: return "Du kommer bli rik inom närmaste framtiden" elif ansnu == 2: return "Du kommer få tur i kärleken" elif ansnu == 3: return "Du kommer att göra en resa" elif ansnu == 4: return "Du kommer att få framgång i ditt yrke" elif ansnu == 5: return "Den närmaste framtiden ser otydlig ut" elif ansnu == 6: return "Olycka väntar dig" elif ansnu == 7: return "Framtiden är mörk..." elif ansnu == 8: return "Hmmm ser inte bra" # resten måste vara inanför while oxå r = random.randint(1,8) fort = getAn(r) print(fort) print("Vill du testa igen (j/n)") val = input() if val == "n": print("hejdå") console.hide_output()
As you can see there is the option to try again print("Vill du testa igen (j/n)")
val = input()
But any button execpt n restarts the program. How can I change it so only j restarts it??? -
If I correctly understand your request:
import random import console def getAn(ansnu): if ansnu == 1: return "Du kommer bli rik inom närmaste framtiden" elif ansnu == 2: return "Du kommer få tur i kärleken" elif ansnu == 3: return "Du kommer att göra en resa" elif ansnu == 4: return "Du kommer att få framgång i ditt yrke" elif ansnu == 5: return "Den närmaste framtiden ser otydlig ut" elif ansnu == 6: return "Olycka väntar dig" elif ansnu == 7: return "Framtiden är mörk..." elif ansnu == 8: return "Hmmm ser inte bra" val = "j" while val == "j": console.clear() # resten måste vara inanför while oxå r = random.randint(1,8) fort = getAn(r) print(fort) print("Vill du testa igen (j/n)") val = input() if val == "n": print("hejdå") console.hide_output()
-
Are your running in Python2 or Python3? The method
input()
is quite different between the two.random.choice()
can be your friend.#!/usr/bin/env python3 import console import random messages = ("Du kommer bli rik inom närmaste framtiden", "Du kommer få tur i kärleken", "Du kommer att göra en resa", "Du kommer att få framgång i ditt yrke", "Den närmaste framtiden ser otydlig ut", "Olycka väntar dig", "Framtiden är mörk...", "Hmmm ser inte bra") val = "j" while val == "j": console.clear() print(random.choice(messages)) print("Vill du testa igen (j/n)") val = input().strip().lower() # <space>J<space> == j print("hejdå") console.hide_output()
-
Im using python 3.
Thx for help. I'll try it and get back to you