Return value in python function [duplicate] - python

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
import urllib,re
def getver():
url='https://github.com/Bendr0id/xmrigCC/releases'
website = urllib.urlopen(url)
html = website.read()
links = re.findall(r'(?<=<a href=")[^"]*\bgcc-win64.zip\b', html)
link=links[0]
version=link.split('/')
ver0=version[5]
return ver0
getver()
I've tried to run the code but it doesnt output anything,instead when I replace return with print it prints out the correct answer which is 1.5.2 .
What am I doing wrong?

You have been fooled by the interactive interpeter's friendly habit of printing out the results of any bare expressions you enter.
This does not happen when you run a program, so you need to ensure you output values specifically by using the print statement.
This is specifically mentioned in a rather obscure portion of the Python documentation dealing with the language's grammar.

Change the last line to:
print(getver())

Related

What does '\' mean when declaring a variable? [duplicate]

This question already has answers here:
What does a backslash by itself ('\') mean in Python? [duplicate]
(5 answers)
What is the purpose of a backslash at the end of a line?
(2 answers)
Closed 1 year ago.
While I was searching code from the internet about YouTube data analysis, I found code like this:
df_rgb2['total_sign_comment_ratio'] = df_rgb2['total_number_of_sign'] / df_rgb2['comment_count']
total_sign_comment_ratio_max = df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], 0).max()
df_rgb2['total_sign_comment_ratio'] = \
df_rgb2['total_sign_comment_ratio'].replace([np.inf, -np.inf], total_sign_comment_ratio_max*1.5)
and I was wondering why the analyst used the expression:
df_rgb2['total_sign_comment_ratio'] = \
because whether I apply that code or not, the result is same.
I tried to find the meaning of '\' but all I have got is how to use '\' when printing out the result.
\ is usually used to make a piece of code go on onto multiple lines. If you where to just press enter and continue to write code a line below for example declaring a variable, it would count as an error.
You use this when you need to tidy up code or when your working window is too small for some reason.
See: https://developer.rhino3d.com/guides/rhinopython/python-statements/

Python check if a variable appears in another variable [duplicate]

This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something

New to Python, trying to do print statements. College textbook is super short not being helpful [duplicate]

This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")

Python finding variable characters in a string with RE? [duplicate]

This question already has answers here:
How to grab number after word in python
(4 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
Hey I need to search for variable data in a console from a page source
The data will be shown like this:
"data":[13,17]
It will vary a lot with the amount of units inside the table. I have tried out several RE expressions, but the closest I have come to a result, is with a fixed amount of units.
self.driver.get("website.com")
apidata = self.driver.page_source
print(apidata)
datasetbasic = re.search('"data":[[0-99,0-99]+', apidata)
print(datasetbasic)
Instead of having it as a fixed amount, how do I capture anything that is inside the data table?
Before you ask, I cannot use xpath or any other selenium calls to capture this data directly from the webpage (I think), because the element is from a graph, where the data is only visible in the actual console.
Any help is appreciated

Execute instructions written in a string [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 2 years ago.
I have a little problem, I have written a for loop as a string.
In PHP, with the help of function exec(), we can run the string which will eventually run the for loop defined inside the string.
Can we do such a thing in Python as well?
By example, I would like run follow it:
string="for i in range(1,(5+1)): print(str(i))"
How to run this in Python?
You can use exec if you want to execute some statements:
code = 'for i in range(1,(5+1)): print(str(i))'
exec(code)
If you want to evaluate an expression and get the value then you can use eval:
value = eval('2+3')
print(value) # 5

Categories

Resources