Jump to content

2 quick questions for python 3.x.x

CookiezFort
Go to solution Solved by olbaze,

I also cnat find out how to make it loop after like it gave a result :/

 

Put lines 3-16 inside a while true block. That'll loop it until you force the program to close. If you want to implement a termination based on a special input, you can use while action != instead.

i know that you can get lower(string) in python 2.x.x but I cant find how to do that in 3.x.x

 

I am asking this because I have this simple calculator, however I want the inputs to be put in any lettercase (Uppercase or lowercase, or a mix of them) so that there will always be the output and people dont get an error when they put different 'case' letters. If i use if lower(action) == "multiplication" I get an error that says.

 

Traceback (most recent call last):
  File "C:\python\basic calculator.py", line 34, in <module>
    main()
  File "C:\python\basic calculator.py", line 7, in main
    if lower(action) == "multiplication":
NameError: name 'lower' is not defined

 

My second question Is how can i make the whole thing loop so i dont have to close the 'calculator' and then have to put run it again?

def main():    action = input("Division, multiplication, addition, substraction, square?")    n1 = float(input("What is your first number? "))     n2 = float(input("What is your second number? "))    if action == "multiplication":         print ("the product is", multiplication(n1, n2))     elif action == "division":         print ("the quotient is", division(n1, n2))      elif action == "addition":         print ("the sum is",addition(n1, n2))     elif action == "substraction":         print ("the difference is", substraction(n1, n2))    elif action == "square":        print (square(n1, n2))                                                                                                                                                  def substraction(num1, num2):     return num1-num2                                                          def addition(num1, num2):    return num1+num2                                                                                                                                             def division(num1, num2):    return num1/num2  def multiplication(num1, num2):     return num1*num2def square(num1, num2):    return num1**num2main()

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

try like this

str = "THIS IS STRING EXAMPLE....WOW!!!";print str.lower();

and you can loop using

while True:    codecodecode    codecodecode    blahblahblah

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

try like this

str = "THIS IS STRING EXAMPLE....WOW!!!";print str.lower();

I get invalid syntax and it highlights str

 

 

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

I get invalid syntax and it highlights str

print is a function in python 3, it should be 

print(str.lower())

 

edit: and no semicolons

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

print is a function in python 3, it should be 

print(str.lower())

 

edit: and no semicolons

 

Ok, however my problem is not in like printing lower case only, but making an 'if' that takes the lowercase of an input string so that people can enter for example MultipliCatioN, or Division, instead of multiplication or division.

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, however my problem is not in like printing lower case only, but making an 'if' that takes the lowercase of an input string so that people can enter for example MultipliCatioN, or Division, instead of multiplication or division.

So instead of printing the result call .lower() on the string that is returned from input.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

So instead of printing the result call .lower() on the string that is returned from input.

 

Ok got it now thanks :)

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

So instead of printing the result call .lower() on the string that is returned from input.

 

I also cnat find out how to make it loop after like it gave a result :/

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

I also cnat find out how to make it loop after like it gave a result :/

 

Put lines 3-16 inside a while true block. That'll loop it until you force the program to close. If you want to implement a termination based on a special input, you can use while action != instead.

I own and use, sorted from newest to oldest: SteelSeries 6Gv2. Microsoft SideWinder X4. Mionix Naos 7000. Zowie EC1 Evo. Microsoft SideWinder X8. Microsoft IntelliMouse Explorer 3.0. Dell U2414H. Samsung P2270H. AKG K273 Pro. Sennheiser HD555. Razer Goliathus Speed Medium. Func 1030 L. Qpad CT Medium.

I used to own: Razer DeathAdder 3G. Razer Krait. IntelliMouse Optical 1.1. SteelSeries QcK.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×