Python Forum
basic random number generator in replace function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
basic random number generator in replace function
#1
Hey, I'm new to python and wanted to know how can I integrate a pseudo-random number generator to a replace function
for example how do I input something like "a) 245 V, b) 736.77 V." and print it with different numbers 3 times
thanks before hand :)
Reply
#2
Without an output example, this is not very clear to me.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Is an example needed? The question is, "Can I use random with replace()?" I take this to mean if we are doing string.replace(old, new) and there are multiple occurrences of "old" in "string", can he somehow use "random" to make a new "new" for each occurrence. I don't see how that can be done.

This obviously doesn't work:
import random
 
a = 'Now is the time for all good men to come to the aid of their country'
b = a.replace('t', random.choice(('A', 'B', 'C')))
print(b)
random.choice gets evaluated and the result is used for "new" in all the replacements.
Output:
Now is Ahe Aime for all good men Ao come Ao Ahe aid of Aheir counAry
I cannot think of a way that you can re-evaluate "new" for each replacement. I think you need to look for another solution to this particular problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,014 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,319 Sep-07-2022, 02:01 AM
Last Post: kaega2
  matrix number assignement to the random indices juniorcoder 4 1,874 Feb-19-2022, 02:18 PM
Last Post: juniorcoder
  Tuple generator, and function/class syntax quazirfan 3 3,761 Aug-10-2021, 09:32 AM
Last Post: buran
  Random coordinate generator speed improvement saidc 0 2,021 Aug-01-2021, 11:09 PM
Last Post: saidc
  Generate random hex number ZYSIA 1 11,319 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  Checking the number of arguments a function takes Chirumer 3 2,113 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Random Number Repeating Tzenesh 5 3,926 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  is there a single function to confine a number between two others? Skaperen 7 2,761 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  Random number generator charlottelol 5 3,136 Nov-10-2020, 10:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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