Python Forum
replace-function for selveral variables..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace-function for selveral variables..
#1
Hello,

This is my first post here..

Using Python 2.7, and I am relatively new to python.

I am trying to find a way to make a function for replacing the same value in several string-variables. As of now I am replacing the value by each string (see below).

The variables p1, p2 and p3 are strings with the current syntax: xxx.xx - example: p1 = 990.21, and I want to find out if they are numeric or not.

    if p1.replace('.','').isdigit():
       pass
    else:
       print 'P1 NOT ok'

    if p2.replace('.','').isdigit():
       pass
    else:
       print 'P2 NOT ok'

    if p3.replace('.','').isdigit():
       pass
    else:
       print 'P3 NOT ok'
I have tried doing the following, but I get an tuple-error: attributeerror 'tuple' object has no attribute 'replace'

if (p1, p2, p3).replace('.','').isdigit():
   pass
else:
   print 'P NOT ok'
The internet has given me no answers so far - could be that I do not know what exactly to search for..
Reply
#2
something like
if not all(p.replace('.','').isdigit() for p in (p1, p2, p3)):
    print 'P NOT ok'
there may be better approach, but we need more information (broad picture)
also, as you are new to python, you should use python3, not python2
or if you want to print specific p
for i, p in enumerate((p1, p2, p3), start=1):
    if not p.replace('.','').isdigit():
        print 'P{} NOT ok'.format(i)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Works like a charm! Thank you! :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print variables in function? samuelbachorik 3 898 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  User-defined function to reset variables? Mark17 3 1,630 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Do I have to pass 85 variables to function? Milfredo 10 4,272 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,465 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  basic random number generator in replace function krug123 2 2,029 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Issues with storing variables outside of a function cerulean747 7 3,694 Apr-30-2020, 08:46 AM
Last Post: DeaD_EyE
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 2,964 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  making a function that writes variables (is possible?) miker2808 3 2,336 Jan-30-2020, 06:27 PM
Last Post: buran
  Beginner problem, replace function with for loop Motley_Cow 9 4,609 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Help trying to replace values in a function Rochense 1 2,298 Apr-01-2019, 09:59 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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