简体   繁体   中英

How to call a function in another function? Python

I have a function that stores code in the parser, but I need the function to work when I press "3" and give me information about the current course.

I attach the code below:

import requests
from bs4 import BeautifulSoup
import time

DOLLAR_RUB = 'https://www.google.com/search?sxsrf=ALeKk01NWm6viYijAo3HXYOEQUyDEDtFEw%3A1584716087546&source=hp&ei=N9l0XtDXHs716QTcuaXoAg&q=%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80+%D0%BA+%D1%80%D1%83%D0%B1%D0%BB%D1%8E&oq=%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80+&gs_l=psy-ab.3.0.35i39i70i258j0i131l4j0j0i131l4.3044.4178..5294...1.0..0.83.544.7......0....1..gws-wiz.......35i39.5QL6Ev1Kfk4'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}

def check_currency():
            full_page = requests.get(DOLLAR_RUB, headers=headers)

            soup = BeautifulSoup(full_page.content, 'html.parser')

            convert = soup.findAll("span", {"class": "DFlfde", "class": "SwHCTb", "data-precision": 2})
            print("Сейчас 1 доллар в рублях: " + convert[0].text)
            time.sleep(10)
check_currency()

class Bank:

    def __init__(self):
        self.bank = ('*********\n739237 долларов\n*********')
        self.person = ('*********\nSergey\nAndrey\nIvan\nAnton.\n*********')
        self.valute = check_currency()

    def money(self):
        return self.bank

    def info(self):
        return self.person

    def valuta(self):
        return self.valute
        
print('Нажмите 1, чтобы посмотреть кол-во денег банка: ')
print('Нажмите 2, чтобы посмотреть кол-во должников: ')
print('Нажмите 3, чтобы узнать курс доллара: ')
print('Нажмите 9, если желаете поддержать автора: ')
print('Нажмите 0, чтобы выйти из программы: ')
print('Нажмите цифру: ')

x = Bank()
y = Bank()
c = Bank()

x.money()
y.info()
c.valuta()

while True:
    getinfo = int(input())
    if getinfo == 1:
        print(x.money())
    elif getinfo == 2:
        print(y.info())
    elif getinfo == 3:
        print(c.valuta())
    elif getinfo == 9:
        print('*********\nВы можете поддержать автора с помощью перевода на его Bitcoin кошелек - 32sdadl7jdS82kdl.\n*********')
    elif getinfo == 0:
        print('*********\nВы вышли из программы.\n*********')
        break
    else:
        print('Вы ввели не ту цифру!')

Instead of initializing a self.valuta field in the __init__ method, let your valuta(self) method invoke the other method:

def check_currency():
    ....
    return convert[0].text

class Bank:

    def __init__(self):
        self.bank = ....
        self.person = ....

    def valuta(self):
        return check_currency()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM