Assigning randomly generated integers to string output in Python

Assigning randomly generated integers to string output in Python



I am working on a random fact generator that takes a randomly generated number (in this case 1 -10) and assigns that number to a string output that tells the user a random fact.



So far this is all I have been able to piece together code wise:


#Greeting
a = "Hello, who am I speaking to?"
print("Welcome, " + input(a) + "!" + " Lets see what fact you rolled...")

#Random Number Generator
import random
for x in range (1):
randomNumber = random.randint(1, 10)
print(randomNumber)

#Loop function
def check_val(randomNumber, title, answer):
while True:
if input(randomNumber) == title:
print(answer)
break
else:
print("I dont know.")

#Facts
def fact():

randomNumber, answer = ,
randomNumber[1] = ([1])
answer[1] = "Fact1"
randomNumber[2] = ([2])
answer[2] = "Fact2"
randomNumber[3] = ([3])
answer[3] = "Fact3"
randomNumber[4] = ([4])
answer[4] = "Fact4"
randomNumber[5] = ([5])
answer[5] = "Fact5"
for k in randomNumber:
check_val(randomNumber[k], k, answer[k])


fact()



When running this code, I get the following output:



Hello, who am I speaking to?j
Welcome, j! Lets see what fact you rolled...
5
[1]



I am not able to take that random integer (5) and assign it to random fact #5.



Any ideas on how to assign these random integers to usable output?






It seems like you've written some of your code without really understanding all the things you're using. One of the most useful techniques for trying to solve a programming problem is rubber duck debugging (en.wikipedia.org/wiki/Rubber_duck_debugging) - which involves explaining what you think the code is doing step by step. Can you edit your question to do that? It will help people trying to answer understand what you want to do, and where you might be going wrong in your understanding.

– Isaac van Bakel
Sep 16 '18 at 18:17





2 Answers
2



Hey bud you got a lot going on in here, you can simple this down using a list to store your facts, heres a better format that you can build off of.
As mentioned by @JonClements we can use random.randrange(len(facts)) this will make our code more adaptable if we want to alter the amount of facts in our list.


list


random.randrange(len(facts))


import random

while True:
name = input("nHello, Who am I speaking to?n")
print(f"nWelcome, name! Lets see what you rolled..."

facts = [
'Fact one',
'Fact two',
'Fact three',
'Fact four',
'Fact five',
'Fact six',
'Fact seven',
'Fact eight',
'Fact nine',
'Fact ten'
]

selection = random.randrange(len(facts))

print(facts[selection])






if your not using the integer, you can just use random.choise(facts)

– Jonas Wolff
Sep 16 '18 at 18:07






@JonasWolff of course, I was considering this while writing this but OP original goal was "Any ideas on how to assign these random integers to usable output?" so I wanted to take the route of passing a random int

– vash_the_stampede
Sep 16 '18 at 18:08


int






@vash_the_stampede careful with your indices here... randint is inclusive of the end range so you'll never pick Fact One (index 0 - remember indices start at 0 in Python) and if you happen to pick 10 as the number, you'll get an IndexError trying to print the fact.... You're probably better off putting the facts first, picking a random number by using selection = random.randrange(len(facts)) and then making sure you add 1 if printing the fact number.

– Jon Clements
Sep 16 '18 at 18:15



randint


IndexError


selection = random.randrange(len(facts))






Capping it's fine... but removing them will cause exceptions... but yeah... as long as they keep it exactly to 10 it'll work... I just figured if you're trying to help the OP learn some best practices you might as well go the whole hog and swap two lines around :)

– Jon Clements
Sep 16 '18 at 18:41






Thank you! I am very new to Python (and programming in general). I appreciate the help and multiple perspectives. Ill see if I can get it to work now.

– JABeal
Sep 17 '18 at 19:38


#Greeting
a = "Hello, who am I speaking to?"
print("Welcome, " + input(a) + "!" + " Lets see what fact you rolled...")

#Random Number Generator
import random
for x in range (1):
randomNumber = random.randint(1, 10)
print(randomNumber) <--- your printing 5

#Loop function
def check_val(randomNumber, title, answer):
while True:
if input(randomNumber) == title: <--- answer argument which were passed was [1] remember so it asks you to write something with the text [1]
print(answer)
break
else:
print("I dont know.")

#Facts
def fact():

randomNumber, answer = , <--- your 5 nol longer exist because you now overwrote it with a dict
randomNumber[1] = ([1])
answer[1] = "Fact1"
randomNumber[2] = ([2])
answer[2] = "Fact2"
randomNumber[3] = ([3])
answer[3] = "Fact3"
randomNumber[4] = ([4])
answer[4] = "Fact4"
randomNumber[5] = ([5])
answer[5] = "Fact5"
for k in randomNumber: <--- # now it's iterating over the dict, and since you wrote 1 first that is most likely to be the one it finds first in the memory though there no order here
check_val(randomNumber[k], k, answer[k])


fact()



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)